Timer in a Windows service - not really working?

Posted by marc_s on Stack Overflow See other posts from Stack Overflow or by marc_s
Published on 2010-04-10T10:06:43Z Indexed on 2010/04/10 10:13 UTC
Read the original article Hit count: 265

I have a Windows NT Service in C# which basically wakes up every x seconds, checks to see if any mail notifications need to be sent out, and then goes back to sleep.

It looks something like this (the Timer class is from the System.Threading namespace):

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);
    }
}

Sending the mail and all works just fine, and the service also wakes up every 10 seconds (in reality, this is a setting from a config file - simplified for this example).

However, at times, the service seems to wake up too quickly....

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

Works fine to 22:50:45.500 - why does it log another entry just 37 milliseconds later??

Here, it seems it's totally out of whack.... seems to wake up twice or even three times every time 10 seconds are over....

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

Any ideas why?? It's not a huge problem, but I'm concerned it might start to put too much load on the server, if the interval I configure (10 seconds, 30 seconds - whatever) seems to be ignored more and more, the longer the service runs.

Have I missed something very fundamental in my service code?? Am I ending up with multiple timers, or something?? I can't seem to really figure it out..... have I picked the wrong timer (System.Threading.Timer) ? There's at least 3 Timer classes in .NET - why?? :-)

© Stack Overflow or respective owner

Related posts about windows-services

Related posts about timer