Creating a Windows Service with a Timer

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

As a seasoned software developer with a long-standing career in C# and Visual Studio, I have had the privilege of working with a number of companies and learning from some of the most talented individuals in the industry. In addition to my professional experience, I have authored multiple books on topics such as C#, Visual Studio, and ASP.NET Core. My passion for programming is unwavering, and I am dedicated to staying current with the latest technology and sharing my expertise with others.