1

我有一个 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 类 - 为什么?:-)

4

3 回答 3

2

实际上,问题似乎是我选择了错误的计时器类。

有了System.Threading.Timer,我有我在原始问题中解释的那些问题。

当我切换到 时System.Timers.Timer,所有这些问题都消失了。

所以这仍然引出了一个问题:为什么.NET 框架中至少存在三个不同Timer的类,它们之间有什么(细微的)区别?

于 2010-04-16T16:06:08.273 回答
1

从上面的代码看,你得到了多个计时器并不明显。但在我看来,情况可能是这样。因为 System.Threading.Timer 实现了 IDisposable 所以不是清空变量,而是应该调用 dispose 方法。

MSDN 说……

当不再需要计时器时,使用 Dispose 方法释放计时器持有的资源。

于 2010-04-12T09:22:38.623 回答
0

那可能是因为消息管道。

有时会发送定时器消息,但处理速度不够快,因此它们被堆叠并按顺序处理。

于 2010-04-10T10:11:44.357 回答