Today I found out that System.Timers.Timer is buggy and doesn’t work when used in a windows service application. Microsoft suggests we use the System.Threading.Timer object instead.

I looked into it, and found that to use the System.Threading.Timer object, you have to supply it with a delegate to a static method that it can call when the timer event is triggered. In my application, that means I would have to change virtually all my methods and variables to being static, not something that I wanted to do. So instead I wrote my own timer, using the System.Threading methods. This is a fairly simple timer system:

using System;
using System.Threading;

class TimerExample
static void Main()
AutoResetEvent arv = new AutoResetEvent(false);
bool Stop = false;
int called = 0;

while(Stop == false)
Console.WriteLine(string.Format("Waited: {0}", called));
if(called == 10)

There’s one thing to keep in mind here; this effectively locks up the current thread until the elapsed time has passed. In the above example, the elapsed time is 5 seconds (5000 milliseconds). It shouldn’t take very much to build this concept into an instantiable class that uses a separate processing thread to prevent locking up your application until the elapsed time, and I’d highly advise that you do so.

Today’s discovery of this bug in a Microsoft system class only serves to validate my belief that you should always fully research any method someone else writes before you use them.