我有一个 C# 中的 Windows NT 服务,它基本上每 x 秒唤醒一次,检查是否需要发送任何邮件通知,然后重新进入睡眠状态。
它看起来像这样(Timer
该类来自System.Threading
命名空间):
public partial class MyService : ServiceBase
{
private Timer _timer;
private int _timeIntervalBetweenRuns = 10000;
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// when NT Service starts - create timer to wake up every 10 seconds
_timer = new Timer(OnTimer, null, _timeIntervalBetweenRuns, Timeout.Infinite);
}
protected override void OnStop()
{
// on stop - stop timer by freeing it
_timer = null;
}
private void OnTimer(object state)
{
// when the timer fires, e.g. when 10 seconds are over
// stop the timer from firing again by freeing it
_timer = null;
// check for mail and sent out notifications, if required - works just fine
MailHandler handler = new MailHandler();
handler.CheckAndSendMail();
// once done, re-enable the timer by creating it from scratch
_timer = new Timer(OnTimer, null, _timeIntervalBetweenRuns, _timeIntervalBetweenRuns);
}
}
发送邮件和一切工作正常,服务也每 10 秒唤醒一次(实际上,这是来自配置文件的设置 - 为本示例简化)。
但是,有时,该服务似乎唤醒得太快了....
2010-04-09 22:50:16.390
2010-04-09 22:50:26.460
2010-04-09 22:50:36.483
2010-04-09 22:50:46.500
2010-04-09 22:50:46.537 ** why again after just 37 milliseconds...... ??
2010-04-09 22:50:56.507
工作正常到 22:50:45.500 - 为什么它在 37 毫秒后记录另一个条目?
在这里,似乎完全失控了……似乎每过10秒就会醒来两次甚至三次……
2010-04-09 22:51:16.527
2010-04-09 22:51:26.537
2010-04-09 22:51:26.537
2010-04-09 22:51:36.543
2010-04-09 22:51:36.543
2010-04-09 22:51:46.553
2010-04-09 22:51:46.553
2010-04-09 22:51:56.577
2010-04-09 22:51:56.577
2010-04-09 22:52:06.590
2010-04-09 22:52:06.590
2010-04-09 22:52:06.600
2010-04-09 22:52:06.600
任何想法为什么?这不是一个大问题,但我担心它可能会开始给服务器带来过多的负载,如果我配置的时间间隔(10 秒、30 秒 - 随便)似乎越来越被忽略,服务运行的时间越长.
我是否错过了我的服务代码中一些非常基本的东西?我最终会使用多个计时器还是什么?我似乎无法真正弄清楚......我是否选择了错误的计时器(System.Threading.Timer
)?.NET 中至少有 3 个 Timer 类 - 为什么?:-)