对于工作中的一个侧面项目,我试图让一段代码在特定时间每24小时运行一次。我的老板让我用一个无限循环代替C#的timer类,这是我正在处理的主要约束。我的问题是,代码将在第一个24小时内运行(即它将在我设置的那一天运行代码),但之后它不会更新。我没有被抛出任何异常或错误,所以我认为这只是我的逻辑问题。设置代码以每24小时运行
这是我现在的代码的要点。
int i = 0;
while (true)
{
DateTime currentdate = DateTime.Now;
String time = currentdate.ToString("HH:mm");
if ((time == "23:50" || time == "23:51") && i == 0)
{
HistoricalAverageCollection HAC = new HistoricalAverageCollection();
HAC.ExecHAC();
HistoricalAverageError HAE = new HistoricalAverageError();
HAE.ExecHAE();
FourWeekAverageCollection FWAC = new FourWeekAverageCollection();
FWAC.ExecFWAC();
FourWeekAverageError FWAE = new FourWeekAverageError();
FWAE.ExecFWAE();
DomainsReturningZeroTwentyFourHours DRZ =
new DomainsReturningZeroTwentyFourHours();
DRZ.ExecDomainsReturningZero();
context.SaveChanges();
//Program should update every 24 horus
i = 1;
Console.Write("Updated The Historical Averages In The Data Base at...");
Console.Write(DateTime.Now);
Console.WriteLine("i is -> {0}", i);
Console.Read();
}
else if (time == "06:00" && i == 1)
{
ReportEmail Report = new ReportEmail();
Report.CreateAndSendReport();
i = 0;
Console.Write("counter reset. I is now -> {0} /n Email Sent",i);
Console.Read();
}
}
代码设置晚上11:50打电话给一堆TSQL存储过程,然后在早上6点发送基于该数据的电子邮件报告。但是,它只会运行一次,我发现自己在一两天后的早晨醒来,看到没有电子邮件正在发送。
任何帮助,将不胜感激:)
你不使用计划任务此的原因吗? – jAC
是否有你的老板不想使用Windows任务计划程序的原因?这是它所做的确切的事情。 – Amy
是否可以使用Windows任务调度程序每24小时运行一次exe? Windows任务计划程序有很多自定义选项,可以让它每x次运行一次并重复该过程。 – WBuck