Datetime windows服务正好在晚上11:59:59运行

Datetime windows服务正好在晚上11:59:59运行,datetime,timer,windows-services,Datetime,Timer,Windows Services,我正在编写一个Windows服务,它将在用户定义的时间将报表导出到excel。用户可以选择使用以下选项运行报告: 选择StartTime和EndTime,以DailyTime作为生成报告的时间 日报 周报 月报 年度报告 注意(我的SQL Server数据每秒更新一次&我已经编写了一个Windows应用程序,用户将从中配置上述内容,并将其保存为xml) 我的windows服务已配置为在TimerInterval 30000运行 我的问题是: 1.如果用户将开始时间设置为晚上10:00,结束时间设

我正在编写一个Windows服务,它将在用户定义的时间将报表导出到excel。用户可以选择使用以下选项运行报告:

  • 选择StartTime和EndTime,以DailyTime作为生成报告的时间
  • 日报
  • 周报
  • 月报
  • 年度报告
  • 注意(我的SQL Server数据每秒更新一次&我已经编写了一个Windows应用程序,用户将从中配置上述内容,并将其保存为xml)

    我的windows服务已配置为在TimerInterval 30000运行

    我的问题是: 1.如果用户将开始时间设置为晚上10:00,结束时间设置为上午12:00,每日时间设置为上午12:00,则以下代码将失败,因为我正在使用system.datetime.now

     DateTime tempDailyTime=DateTime.ParseExact(DailyTime,"HH:mm:ss tt",CultureInfo.InvariantCulture);
                        string curTime = System.DateTime.Now.ToString("HH:mm:ss tt");
                        DateTime tempCurrentTime = DateTime.ParseExact(curTime, "HH:mm:ss tt", CultureInfo.InvariantCulture);
                        if (tempCurrentTime >= tempDailyTime)
                        {
    
                            string tempFilename=Convert.ToString(tempDailyTime.TimeOfDay).Replace(":","-")+".xlsx";
                            if (!File.Exists(tempDir + "\\DailyTime" + "\\" + ReportName + "_" + tempFilename))
                            {
                                GenerateReportWithStartTimeandEndTime(ReportName,ReportID,ConnectionString,ReportColumnName,ReportBQuery,StartTime,EndTime,DailyTime,tempDir+"\\DailyTime",tempFilename);
                            }
                        }
    
  • 为了生成每日、每周、每月、每年的报告,我指定了运行报告的计划时间

    DateTime scheduleTime=新的日期时间(currentTime.Year、currentTime.Month、currentTime.Day、23、59、59、999)

  • 在到达以下代码部分之前的计时器经过之后,如果datetime发生更改,它将生成一个空白报告。 我的日报生成代码如下:

     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)
                            {
                                //GenerateDailyReport(ReportName, ReportID, ConnectionString, ReportColumnName, ReportBQuery, StartTime, EndTime, DailyTime, tempDir + "\\DailyTime", tempFilename);
                            }
    
                        }
                        if (Weekly == "true")
                        {
                            //GenerateWeeklyReport(ReportName, ReportID, ConnectionString, ReportColumnName, ReportBQuery, StartTime, EndTime, DailyTime, tempDir + "\\DailyTime", tempFilename);
                        }
                        if (Monthly == "true")
                        {
                            //GenerateMonthlyReport(ReportName, ReportID, ConnectionString, ReportColumnName, ReportBQuery, StartTime, EndTime, DailyTime, tempDir + "\\DailyTime", tempFilename);
                        }
                        if (Yearly == "true")
                        {
                            //GenerateYearlyReport(ReportName, ReportID, ConnectionString, ReportColumnName, ReportBQuery, StartTime, EndTime, DailyTime, tempDir + "\\DailyTime", tempFilename);
                        }
    
    if(每日==“真”)
    {
    DateTime currentTime=DateTime.Now;
    int intervaltoeferse=0;
    DateTime scheduleTime=新的日期时间(currentTime.Year、currentTime.Month、currentTime.Day、23、59、59、999);
    
    如果(currentTime这还不够清楚。计时器的作用是什么?如果您想在特定时间执行任务,应将计时器间隔设置为1秒,并保持监视系统时间,直到系统时间达到您要执行任务的时间。我的计时器设置为30秒。每隔30秒,它将运行服务并执行作业。每日平均每24小时一次。每周7天每月一次,基于月初和月底,年度平均开始和年底。我正在考虑以秒为单位的间隔时间,并检查哪一个是错误的。相反,我添加了私有日期时间_lastRun=DateTime.Now.AddDays(-1);以及if(_lastRun.Date1天。但对于e当前实例上次运行时间为12.00.30AM。同样,第二天计时器将在12:01:00AM检测任务。您可以通过将上次运行时间覆盖到12:00:00AM来最小化此问题。
    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfAutoReportXML>
      <AutoReportXML ReportName="oks" ReportID="64" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t1.id, t1.pt100, t1.pt200, t1.pt300, t1.inverter1, t1.voltage1, t1.power1, t1.inverter2, t1.voltage2, t1.power2, t1.cdt  FROM ab_Test t1 WHERE  t1.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="18:00:00 PM" EndTime="22:00:00 PM" DailyTime="22:01:00 PM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
      <AutoReportXML ReportName="ShiftC" ReportID="63" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t1.id, t1.pt100, t1.pt200, t1.pt300, t1.inverter1, t1.voltage1, t1.power1, t1.inverter2, t1.voltage2, t1.power2, t1.cdt  FROM ab_Test t1 WHERE  t1.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="22:00:00 PM" EndTime="06:00:00 AM" DailyTime="18:01:00 PM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
      <AutoReportXML ReportName="ShiftB" ReportID="62" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t1.id, t1.pt100, t1.pt200, t1.pt300, t1.inverter1, t1.voltage1, t1.power1, t1.inverter2, t1.voltage2, t1.power2, t1.cdt  FROM ab_Test t1 WHERE  t1.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="14:00:00 PM" EndTime="22:00:00 PM" DailyTime="22:01:00 PM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
      <AutoReportXML ReportName="shiftA" ReportID="61" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t1.id, t1.pt100, t1.pt200, t1.pt300, t1.inverter1, t1.voltage1, t1.power1, t1.inverter2, t1.voltage2, t1.power2, t1.cdt  FROM ab_Test t1 WHERE  t1.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="06:00:00 AM" EndTime="14:00:00 PM" DailyTime="14:01:00 PM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
      <AutoReportXML ReportName="testreport1" ReportID="59" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t0.testid, t0.pt500, t0.pt600, t0.cdt  FROM sampletest t0 WHERE  t0.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="17:00:00 PM" EndTime="11:59:59 PM" DailyTime="00:01:00 AM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
      <AutoReportXML ReportName="testreport" ReportID="58" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t0.testid, t0.pt500, t0.pt600, t0.cdt  FROM sampletest t0 WHERE  t0.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="09:00:00 AM" EndTime="17:00:00 PM" DailyTime="17:01:00 PM" Daily="true" Weekly="true" Monthly="true" Yearly="true" Loc="C:\Users\khasim\Desktop\testAutoGenerated" />
      <AutoReportXML ReportName="somereport" ReportID="57" ConnectionString="server='KHASIM-PC\SQLEXPRESS';uid='sa';pwd='khasim123';database='ReportMgr'" ReportBQuery="SELECT t0.testid, t0.pt500, t0.pt600, t0.cdt  FROM sampletest t0 WHERE  t0.cdt BETWEEN @VALUE1 and @VALUE2" ReportColumnName="cdt" StartTime="02:00:00 AM" EndTime="05:37:47 AM" DailyTime="06:37:47 AM" Daily="True" Weekly="True" Monthly="True" Yearly="True" Loc="C:\Users\khasim\Desktop\testAutoGenerated">57</AutoReportXML>
    </ArrayOfAutoReportXML>