午夜运行窗口服务 [英] running windows service at midnight

查看:151
本文介绍了午夜运行窗口服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码每5分钟执行一次,在5分钟内,如果日期更改,我的代码将不会运行。如何处理这个,请帮助

my code executes every 5 minutes, within the 5 minutes if date changes my code will not run. how to handle this, please help

我想我需要检查当前time.addminutes(5),并检查日期
是否更改,如果日期更改然后需要设置定时器,以便我的代码可以
运行可以任何一个帮助他如何实现这个

I think i need to check currenttime.addminutes(5) and check if date changes and if date changes then need to set timer so that my code can run can any one help he how to implement this

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

                                }


推荐答案

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

The Problem with my above code is that I was trying to check elapsed time and compare it to zero. Instead, if the date change is identified, and check it with the current date for date change which obviously means the time has elapsed so the code will run and create report successfully.

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

                            }

这篇关于午夜运行窗口服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆