2017-07-10 72 views
0

我已經創建了一個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

回答

1

如果你想在接下來的具體時間安排百達的動作,你也可以解決它像下面這樣。

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

感謝您的回覆。我會檢查這一點。 –

相關問題