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 and Microsoft MVP from South Africa. He loves all things Technology and is slightly addicted to Twitter and Jimi Hendrix. Apart from writing code, he also enjoys writing human readable 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 yet to master.

Similar Articles

  • Lloyd Derbyshire

    I see you wrote this over a year ago but I thank you very much for explaining why I just wasted hours trying to get the timer going when it was never going to fire. When they do things like this I think that Microsoft really want us to fail at developing on their stack. What use is the Forms timer in the Context of writing a Service . . . we shouldn’t even see it!!!

    • Dirk Strauss

      Hi Lloyd

      I couldn’t agree more. Why even offer a control for use that clearly will never work for a Service. As a developer, you almost expect the controls you see to be specific to what you are currently developing. Thank you for your comment. I’m glad that it has helped a fellow dev.

      Good luck!

      • Lloyd Derbyshire

        Cheers Dirk. Thanks for the encouragement.

        I just go the damn thing working (at last), polling and sending an email alert.

        Keep up the good work mate.

        • Dirk Strauss

          Awesome dude. Glad you got sorted! I love it when I solve code on a Friday 🙂

  • Andy F

    Dirk – Thanks so much! After reading MS’s instructions for doing this where (as usual) they refer to menus that arent there, toolbars that dont exist, and terminology more confusing than enlightening, your article was great to come across – clear, concise, and complete,. Many thanks for the assist with this!

    • Dirk Strauss

      I am very glad that this article helped you! Thank you too for the compliments.

  • Paul

    Thankyou. I was pulling my hair out.!

    • Dirk Strauss

      Hi Paul

      I’m really happy that the post on the subject helped. 🙂

  • Hopsis

    Thanks a lot

    • Dirk Strauss

      You’re welcome 🙂

  • Sachin

    Thanks… very much

  • M Selvaraaj Prabu

    Wonderful post! I was struggling to resolve this issue and you made my day! Thank you once again!

  • stk33

    Why you even can is beyond me. Please, if anyone can provide a reason, leave a comment.

    Actually, you provided it by yourself: after you customized the tools and added ‘right” timer, they both appeared in the tools exactly alike, so naturally, most reasonable expectation of a programmer is that Microsoft would update old winforms timer to the new times, rather than leave old incompatible version and create another with exactly the same name and even icon. Apparently it’s beyond anyone why they did not – and this has been going on already for X releases of Visual Studio.

    That is explanation #1. Explanation #2 is that Visual Studio does allow you to drop “wrong” timer on the service designer panel. Again, one would expect that what’s allowed to be placed, should work, and what does not work, should not be allowed to be dropped.

Top