我的代碼每5分鐘執行一次,如果日期發生變化,5分鐘內我的代碼將不會運行。如何處理這個問題,請大家幫忙午夜運行windows服務
我想我需要檢查currenttime.addminutes(5),並檢查日期 變化,如果日期更改則需要設置定時器,使得我的代碼可以運行 任何一個可以幫助他如何實現這一
if (Daily == "true")//run daily at 11:59:59
{
DateTime currentTime = DateTime.Now;
int intervalToElapse = 0;
DateTime scheduleTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 23, 59, 59, 999);
if (currentTime <= scheduleTime)
intervalToElapse = (int)scheduleTime.Subtract(currentTime).TotalSeconds;
else
intervalToElapse = (int)scheduleTime.AddDays(1).Subtract(currentTime).TotalSeconds;
_DailyTimer = new System.Timers.Timer(intervalToElapse);
if (_DailyTimer.Interval == 0)//if date changes this will be false and the code will not run
{
string tempFilename = Convert.ToString(tempDailyTime.TimeOfDay).Replace(":", "-") + ".xlsx";
if (!File.Exists(tempDir + "\\Daily" + "\\" + ReportName + "_" + tempFilename))
{
GenerateDailyReport(ReportName, ReportID, ConnectionString, ReportColumnName, ReportBQuery, "00:00:00", "23:59:59", tempDir + "\\Daily", tempFilename);
}
}
}
「午夜」部分在哪裏?什麼是'每日'? – Raptor
每天是每24小時,如果(_DailyTimer.Interval == 0)秒 – Tan
我已經張貼我的問題清楚地在http://stackoverflow.com/questions/35704478/windows-service-run-at-exactly-115959-pm – Tan