2013-10-11 3 views
0

다른 프로세스를 제어하는 ​​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); 

아래와 같이 다차원 사전을 가지고 그리고 다음 동작에 따라 행동하는 간단한 타이머를 사용하기 전에 "를 OnOff"타임 스팬의 가장 가까운에 의해 "프로세스"사전을 정렬 할 수 있습니다 TimeSpan이 도착할 때의 프로세스입니다.

이 계획의 문제점은 TimeSpans를 정렬하고 가장 가까운 TimeSpan이 정렬 기준으로 포함 된 "시작"/ "중지"작업을 찾는 것입니다. 또한 가장 가까운 TimeSpan이 포함 된 각 프로세스의 다른 동작이 될 수 있다는 사실도 덧붙입니다.

다른 사람이 같은 유형의 결과를 얻는 더 간단한 방법을 생각할 수 있습니까?

답변

0

을 목록에 추가해야하는 경우 Dictionary<string,ProcessInfo> schedule = new Dictionary<string,ProcessInfo>(); 그냥 processinfo의 새로운 개체를 만드는 간단한 사전의 구조를 유지 일정 정보는 항상 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; 
     } 


    } 
0

왜 그냥 구조

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

을 유지하고로

+0

클래스/구조체와 같이 데이터를 저장하는 여러 가지 방법이 있지만 시간과 데이터를 정렬해야하는 문제는 여전히 남아 있습니다. – David

+0

당신은 정렬 함수/알고리즘을 주기적으로 실행하고 인덱스로 실행하는 프로세스를 저장할 수 있습니다. – Moiz

+0

미안하지만 ProcessInfo를 유지하는 것보다 processInfo를 ** 유지해야합니다. ** processInfo의 ** 배열 **을 유지해야합니다. 간단한 정렬 알고리즘을 사용합니다. – Moiz

관련 문제