我是Windows服务的新手。我创建了一个具有多个Scheduler的Windows服务。每个调度程序应在不同的时间间隔内运行。请帮助我知道它是否可以在单个WS中工作。
我的代码在这里。
private Timer Schedular1;
private Timer Schedular2;
public void ScheduleService()
{
try
{
Schedular1 = new Timer(new TimerCallback(SchedularCallback1));
Schedular2 = new Timer(new TimerCallback(SchedularCallback2));
this.WriteToFile("Simple Service Mode: " + mode + " {0}");
//Set the Default Time.
DateTime scheduledTime = DateTime.MinValue;
//Get the Scheduled Time from AppSettings.
scheduledTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]);
if (DateTime.Now > scheduledTime)
{
//If Scheduled Time is passed set Schedule for the next day.
scheduledTime = scheduledTime.AddDays(1);
}
TimeSpan timeSpan = scheduledTime.Subtract(DateTime.Now);
string schedule = string.Format("{0} day(s) {1} hour(s) {2} minute(s) {3} seconds(s)", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
this.WriteToFile("Simple Service scheduled to run after: " + schedule + " {0}");
//Get the difference in Minutes between the Scheduled and Current Time.
int dueTime1 = Convert.ToInt32(timeSpan.TotalMilliseconds);
int dueTime2 = Convert.ToInt32(timeSpan.TotalMilliseconds) + 100000;
//Change the Timer's Due Time.
Schedular1.Change(dueTime1, Timeout.Infinite);
Schedular2.Change(dueTime2, Timeout.Infinite);
}
catch(Exception ex)
{
WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);
//Stop the Windows Service.
using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
{
serviceController.Stop();
}
}
}
private void SchedularCallback1(object e)
{
this.WriteToFile("I'm in SchedularCallback1");
this.ScheduleService();
}
private void SchedularCallback2(object e)
{
this.WriteToFile("I'm in SchedularCallback2");
this.ScheduleService();
}
我想知道这个Scheduler1,Scheduler2是否能正常工作而没有任何问题?