c#在特定时间每天致电窗口服务

问题描述:

我已经创建了一个windows service,它将每天在12 P.P.但由于一些问题,我不明白为什么它被称为在午夜12 A.M.c#在特定时间每天致电窗口服务

这里是我的代码:

private Timer _timer = null; 
private DateTime _scheduleTime; 
private static int FixHours = 12;//12 P.M. 

protected override void OnStart(string[] args) 
{ 
    try 
    { 
     _timer = new Timer(); 
     //check for time, if service started in morning before fix hours then service call should be on fixhours else next day 
     if (DateTime.Now.TimeOfDay.Hours < FixHours) 
     { 
      var staticDateTime = DateTime.Now.Date; 
      staticDateTime = staticDateTime.AddHours(FixHours).AddMinutes(0).AddSeconds(0); 
      _timer.Interval = staticDateTime.Subtract(DateTime.Now).TotalMilliseconds; 
      Log.Debug("Schedule Time:- " + staticDateTime.ToString()); 
     } 
     else 
     { 
      // Schedule to run once a day at 12 P.M. 
      _scheduleTime = DateTime.Today.AddDays(1).AddHours(FixHours); 
      _timer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalMilliseconds; 
      Log.Debug("Schedule Time:- " + _scheduleTime.ToString()); 
      Log.Debug("Total Milisecond:- " + _timer.Interval.ToString()); 
     } 
     _timer.Elapsed += Timer_Elapsed; 
     _timer.Enabled = true; 
    } 
    catch (Exception ex) 
    { 
     Log.Error(ex); 
    } 
} 
private async void Timer_Elapsed(object sender, ElapsedEventArgs e) 
{ 
    Log.Debug("Service Called:-" + e.SignalTime.ToString());    
    // 1. If tick for the first time, reset next run to every 24 hours 
    double totalInterval = FixHours * 60 * 60 * 1000; 
    if (_timer.Interval != totalInterval) 
     _timer.Interval = totalInterval; 
} 

这里我声明修复小时下午12点。在OnStart方法中,我编写了用于识别何时调用服务的代码。如果我在12 PM之前立即启动服务,那么它会调用该功能,如果我在12 PM之后启动,那么它应该在明天拨打12 PM

但有时它在午夜时间打电话。我不知道我在做什么错。

这是我的日志:

2017-07-05 11:10:42,096 [4] DEBUG Schedule Time:- 7/5/2017 12:00:00 PM 
2017-07-05 12:00:00,326 [6] DEBUG Service Called:-7/5/2017 12:00:00 PM 
2017-07-05 15:47:18,097 [4] DEBUG Schedule Time:- 7/6/2017 12:00:00 PM 
2017-07-05 15:47:18,113 [4] DEBUG Total Milisecond:- 72761917.9899 
2017-07-06 12:00:03,981 [6] DEBUG Service Called:-7/6/2017 12:00:03 PM 
2017-07-07 00:00:05,745 [1441] DEBUG Service Called:-7/7/2017 12:00:05 AM 
2017-07-07 12:00:07,873 [1860] DEBUG Service Called:-7/7/2017 12:00:07 PM 
2017-07-08 00:00:09,906 [422] DEBUG Service Called:-7/8/2017 12:00:09 AM 
2017-07-08 12:00:12,031 [1019] DEBUG Service Called:-7/8/2017 12:00:12 PM 
2017-07-09 00:00:14,299 [2282] DEBUG Service Called:-7/9/2017 12:00:14 AM 
2017-07-09 12:00:16,334 [843] DEBUG Service Called:-7/9/2017 12:00:16 PM 
2017-07-10 00:00:18,279 [2972] DEBUG Service Called:-7/10/2017 12:00:18 AM 
+0

你可以从看[Quartz.NET FAQ](https://www.quartz-scheduler.net/documentation/faq.html)受益于有关Windows和时间怎么不得到一些想法沿。如果系统时钟已更新,您的计时器将会出错。 DST?最好你的代码会在所请求的时间之后或之后运行,这取决于其他任务。 – HABO

如果你想在接下来的具体时间安排百达的动作,你也可以解决它像下面这样。

var now = DateTime.Now; 
var scheduledTime = new DateTime(now.Year, now.Month, now.Day, FixHours, 0, 0); 
if (scheduledTime < now) 
    scheduledTime = scheduledTime.AddDays(1); 

var timeout = scheduledTime - now; 

var timer = new Timer(timeout.TotalMilliseconds); 
timer.Enabled = true; 
timer.Elapsed += Timer_Elapsed; 
+0

感谢您的回复。我会检查这一点。 –