2016-03-01 110 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