【问题标题】:running windows service at midnight在午夜运行 Windows 服务
【发布时间】: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


【解决方案1】:

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

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

                            }

【讨论】:

  • Stack Overflow 不是您的个人密码板。没有解释的答案可能会被删除
  • 对不起,我没有意识到这一点。我已经添加了解释。谢谢
猜你喜欢
  • 2016-01-13
  • 2017-08-07
  • 2021-10-15
  • 2017-10-18
  • 2015-09-17
  • 2016-08-08
  • 2012-08-09
  • 2014-12-06
  • 1970-01-01
相关资源
最近更新 更多