2016-03-01 128 views
0

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

           } 
+0

“午夜”部分在哪里?什么是'每日'? – Raptor

+0

每天是每24小时,如果(_DailyTimer.Interval == 0)秒 – Tan

+0

我已经张贴我的问题清楚地在http://stackoverflow.com/questions/35704478/windows-service-run-at-exactly-115959-pm – Tan

回答

0

与我上面的代码的问题是,我试图检查经过的时间,并将其与零。相反,如果日期更改被识别,并检查它与日期更改的当前日期,这显然意味着时间已过,因此代码将运行并成功创建报告。

private DateTime _lastRun = DateTime.Now.AddDays(-1); 
    if (Daily == "true") 
          { 
           //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 (_lastRun.Date < DateTime.Now.Date) 
           { 
            DateTime schTime = new DateTime(_lastRun.Year, _lastRun.Month, _lastRun.Day, 23, 59, 59, 999); 
            string tempFilename = Convert.ToString(tempDailyTime.TimeOfDay).Replace(":", "-") + ".xlsx"; 
            if (!File.Exists(tempDir + "\\Daily" + "\\" + ReportName + "_" + tempFilename)) 
            { 
             GenerateDailyReport(ReportName, ReportID, ConnectionString, ReportColumnName, ReportBQuery,Convert.ToString(_lastRun.Date), Convert.ToString(schTime), tempDir + "\\Daily", tempFilename); 
_lastRun = DateTime.Now; 
            } 
           } 
           //} 

          } 
+1

堆栈溢出不是您的个人代码簿。没有解释的答案可能会被删除 – Raptor

+0

对不起,我没有意识到这一点。我已经添加了解释。谢谢 – Tan