You are here
Home > Programming > Creating a Windows Service with a Timer

Creating a Windows Service with a Timer

Fallback Image

Okay, you probably all know this, but somewhere someone doesn’t. And I thought it might be a nice article to post. Have you created a Windows Service Timer that isn’t working? The reason for this is because you probably added a Windows Forms Timer to your Service instead of a System Timer. So for future reference, this is what I did:

Windows Service Timer Workaround

In your toolbar, you will see the Timer control under Components. Do not add this timer to your Service. (Why you even can is beyond me. Please, if anyone can provide a reason, leave a comment.) Right click on the toolbar as follows and select Choose Items:

Windows Service Timer

Click on the Namespace Tab to sort the .NET Framework Components.

Windows Service Timer Toolbox Items

Scroll down to the System.Timers Namespace and check the check box to the left.

Windows Service Timer Item Added

When you return to the Toolbar, you will notice two Timer Controls.

Windows Service Timer Toolbar Added

Right Click on each one and Rename the Forms Timer to something like “WinForms Timer” and the Service Timer to “Service Timer”.

Windows Service Timer Toolbar Rename

Your Toolbox should look as follows.

Windows Service Timer Renamed

If you drag an instance of each timer on the Service Designer and select the properties of each, the difference is evident. The Forms timer is a System.Windows.Forms.Timer class and will not work in your Service.

Windows Service Timer Properties

The Service Timer is a System.Timers.Timer class and should be used in your Windows Service.

Windows Service Timer Properties

For more information on using Timers in a Windows Service, take a look at Using Timers in a Windows Service posted by Mark Strawmyer. It is an older post, but I still think that it is relevant and could be a source of useful information. As always, if you have any comments, please consider taking part in the discussion below.

Dirk Strauss
Dirk is a Software Developer from South Africa. He loves all things Technology and is slightly addicted to Jimi Hendrix. Apart from writing code, he also enjoys authoring books and articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has almost mastered.
https://dirkstrauss.com

Similar Articles

Top