Monday, September 10, 2007

Windows .NET Service Program Does Not Fire Timer

Scenario:

  • You started to write a Windows Service using C# and Visual Studio 2005.
  • You dragged a Timer object into the service component's design surface.
  • You wrote a Timer handler object.
  • Timer never fires.

Solutions:

The fact that VS 2005 allows you to drag the timer (or other objects) is very misleanding. It actually never works because timer event will never be fired. In theory they (Visual Studio) should not allow you to do that. (I swear it worked in 2003 but that's the past.)

There are numerous postings, messages and such all over the places on this. It took me half a day to figure this out.

But the reason why the timer event does not fire is because under the Service execution model, it does have a "message pump" executing like in windows desktop apps.

The only way that I know it works is to use System.Threading object, launch a thread and then you block the thread by N milliseconds using the sleep function within the thread to achive the same result.

If you want an example of this, I can elaborate here. Just write a comment.

No comments: