在午夜运行 Windows 服务

本文关键字:服务 Windows 运行 午夜 | 更新日期: 2023-09-27 18:36:59

我的代码每 5 分钟执行一次,如果日期更改,则在 5 分钟内执行一次,我的代码将无法运行。 如何处理这个问题,请帮忙

我想我需要检查当前时间.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);
                                        }
                                    }
                                }

在午夜运行 Windows 服务

我上面的代码的问题在于我试图检查经过的时间并将其与零进行比较。相反,如果确定了日期更改,并将其与当前日期进行检查以了解日期更改,这显然意味着时间已经过去了,因此代码将运行并成功创建报告。

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;
                                    }
                                }
                                //}
                            }