【问题标题】:Timer in a Windows service - not really working?Windows 服务中的计时器 - 没有真正工作?
【发布时间】:2010-04-10 10:06:43
【问题描述】:

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

【问题讨论】:

    标签: windows-services timer timer-jobs


    【解决方案1】:

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

    使用System.Threading.Timer,我遇到了我在原始问题中解释的那些问题。

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

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

    【讨论】:

      【解决方案2】:

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

      MSDN 说...

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

      【讨论】:

        【解决方案3】:

        这可能是因为消息管道。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-08-16
          • 1970-01-01
          • 1970-01-01
          • 2019-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多