我的代码每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