【发布时间】:2017-08-18 01:42:39
【问题描述】:
我创建了 .net Windows 服务,并安排它每 5 分钟运行一次。 此 Windows 服务包含发送电子邮件并在发送电子邮件后更新表格的代码。
首先我发送所有电子邮件,然后批量更新数据库中的所有记录。所以我的问题是我将间隔设置为 5 分钟,它是否会破坏我的场景,例如发送电子邮件需要 5 分钟,然后是计时器在不更新数据库的情况下停止,或者继续执行直到完全执行任务。
_timer = new Timer(5* 60 * 1000);
_timer.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
_timer.AutoReset = true;
_timer.Start();
.......................
{//send one email at a time ..this is in loop
Library.SendEmail(edtls);
ds.Tables["EDts"].Rows[i]["Sent"] = 1;
ds.Tables["EDts"].Rows[i]["Status"] = "Sent";
ds.Tables["EDts"].Rows[i].EndEdit();
}..how about timer stop execution here(it sends the emails but doesn't update the table)
ds.Update(EDts, "EmailDts");//updating table here
【问题讨论】:
-
哪个
Timer类? .NET 定义了 4 种不同的,有可能使用不正确的 -
它不是控件。 AutoReset = true 非常危险,并且 将 导致 Elapsed 事件处理程序运行,即使前一个滴答尚未完成。这很少有好的结局。修复简单,使用 false 并在事件处理程序末尾设置 Enabled = true。始终在事件处理程序中使用 try/catch 以确保不吞噬异常。
标签: c#