views:

128

answers:

4

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?? :-)

A: 

That's probably because the message pipeline.

Sometimes timer messages are sent, but not handled quickly enough, so they are stacked and processed in sequence.

Paulo Santos
+1  A: 

it is not obvious from looking at the code above, that you are getting multiple timers. but in my opinion that may be the case. because System.Threading.Timer implements IDisposable so instead of nulling out the variable you should call the dispose method instead.

MSDN says...

When a timer is no longer needed, use the Dispose method to free the resources held by the timer.

jake.stateresa
+2  A: 

Actually, the issue seems to be I picked the wrong timer class.

With the System.Threading.Timer, I have those issues I explained in my original question.

When I switched to System.Timers.Timer, all those issues went away.

So this still begs the question: why on earth are there at least three different Timer classes in the .NET framework, and what are the (subtle) differences between them?

marc_s
A: 

Can you please share your modified code ?

newbee2010