【发布时间】:2016-03-01 04:11:31
【问题描述】:
我的代码每 5 分钟执行一次,如果日期更改,我的代码将在 5 分钟内无法运行。如何处理,请帮忙
我想我需要检查 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);
}
}
}
【问题讨论】:
-
“午夜”部分在哪里?
Daily是什么? -
daily 是每 24 小时一次,如果 (_DailyTimer.Interval == 0) 秒
-
我已经在stackoverflow.com/questions/35704478/… 上清楚地发布了我的问题
标签: c# datetime windows-services