【问题标题】:How to sort internal value of multidimensional dictionary如何对多维字典的内部值进行排序
【发布时间】:2013-10-19 10:31:05
【问题描述】:

我正在创建一个 Windows 服务,它控制着一堆其他进程,但内部进程需要按照自己的时间表运行。 每个时间表都相对简单,只需选择它们在一周中的哪几天运行、进程应该在什么时候开始以及应该在什么时候停止。

我的想法是有一个多维字典,如下所示

Dictionary<string, TimeSpan> OnOff = new Dictionary<string,TimeSpan>();
OnOff.Add("Start", TimeSpan.Parse("09:00"));
OnOff.Add("Stop", TimeSpan.Parse("17:00"));

Dictionary<string, Dictionary<string, TimeSpan>> Process = new Dictionary<string,Dictionary<string,TimeSpan>>();
Process.Add("Process1", OnOff);

Dictionary<DayOfWeek, Dictionary<string, Dictionary<string, TimeSpan>>> schedule = new Dictionary<DayOfWeek,Dictionary<string,Dictionary<string,TimeSpan>>>();
schedule.Add(DayOfWeek.Monday, Process);

并在使用简单计时器在 TimeSpan 到达时对进程的下一个操作采取行动之前,按最接近的“OnOff”TimeSpan 对“Process”字典进行排序。

我对这个计划的问题是 TimeSpan 的排序,并找出哪个“开始”/“停止”动作包含最接近的 TimeSpan 进行排序。再加上它可能是包含最接近 TimeSpan 的每个进程的不同操作。

或者谁能想到一种更简单的方法来实现相同类型的结果?

【问题讨论】:

    标签: c# dictionary multidimensional-array scheduling


    【解决方案1】:

    为什么不只保留一个结构

    `class ProcessInfo
    {
    String ProcessID;
    TimeSpan startTime;
    TimeSpan endTime;
    }`
    

    并保持字典的结构简单 Dictionary&lt;string,ProcessInfo&gt; schedule = new Dictionary&lt;string,ProcessInfo&gt;(); 当您需要添加到列表时,只需创建一个 processinfo 的新对象

    【讨论】:

    • 我很欣赏有不同的方法来存储数据 - 例如在类/结构中,但问题仍然存在,必须对数据进行排序并考虑星期几。
    • 您可以定期运行排序函数/算法并将要运行的进程存储在索引中
    【解决方案2】:

    由于计划信息始终存在于 App.Config 文件中,因此我决定不需要将其存储在其他地方。 我处理调度的方法是找到所有需要在最近的将来采取行动的进程,并只存储这些进程的详细信息、调度时间和所需的行动。

    然后我简单地运行了一个计时器,并在预定时间执行启动或停止操作。 处理完所有这些进程后,我再次查看 App.Config 并找到下一个要处理的进程列表。

        // The method that will be called when the schedule thread is started
        private void ProcessSchedule()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
    
            // work out the length of time between now and the schedule time
            string today = DateTime.Now.ToString("dd.MM.yyy");
            DateTime abaseTime = Convert.ToDateTime(today);
            TimeSpan scheduleTime = schedules.First().Value.ActionTime;
            DateTime actionTime = abaseTime.Add(scheduleTime);
    
    
            // log the schedule is starting
            if (log.IsInfoEnabled) log.Info(String.Format("Schedule created. Next action will take place at {0}", actionTime.ToString()));
    
            while (Running)
            {
                // this kills this process
                if (threadStop.WaitOne(0))
                {
                    if (log.IsInfoEnabled) log.Info(String.Format("Schedules cancelled"));
                    break;
                }
    
                if (DateTime.Now < actionTime)
                {
                    //sleep 5 seconds before checking again. If we go any longer we keep our service from shutting down when it needs to.
                    threadStop.WaitOne(5000);
                    continue;
                }
    
                // tell the service control that it can action the schedules now
                ServiceControl.actionSchedule.Set();
    
                break;
            }
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-25
      • 2012-07-29
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 2022-11-14
      相关资源
      最近更新 更多