Using Timers in a C# Windows Service

For the most part, writing a windows service in Visual Studio.net is only marginally different than writing a windows application.  You may have difficulty attempting to implement some components you’ve learned to rely on. One such component is the timer.

In a windows application, you simply drag a Timer from the Toolbox and implement as normal.  Curiously, you can do the same in a service–it just doesn’t work.  This is because the timer in the toolbox is located in System.Windows.Forms. So how do you do it? It’s really quite simple.

First, add the following line to the top of your Windows Service project:

using System.Timers;

Next, add this line to top of your class (the section starting with public partial class..)

Timer timer1= new Timer();

Obviously you may want to pick a more descriptive name…just be consistent. Next we need to create the method that will fire each time the timer reaches 0.  

private void timer1_Elapsed(object sender, EventArgs e)
{
      //Some awesome code!
}

Lastly, we need to bind this method to the timer’s elapsed event and finish implementing the timer.  Find your service’s OnStart() method and add something like:

timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Start();

The first line above tells the service to run timer1_Elapsed everytime the timer1.Elapsed event is raised. The second line sets the timer1′s interval to 5 seconds (1000 milliseconds = 1 second).  Lastly we enable the timer and start it.

You may want to add this to your OnStop() method for completeness:

timer1.Enabled = false;

That’s all there is to it.  This should be all you need to know to implement a timer in your service.

  • http://notyet mohamed

    thank you VERY MUCH
    IT WAS SO HELPFUL

    • Shaik Raffi

      working well but it is not working for 60 miniutes means hour kinds of stuff please help

  • Thilo

    Nice article.

  • Pingback: Windows Service + installer « Blogging with a mission

  • Pingback: 用C#寫Windows服務(Wndows service)含Timer及安裝專案 | Austin's Note

  • pertneer

    Thanks so much. I created a service following a series of articles I found at another site. The articles were very good and explain how a service worked. The only problem was that the service didn’t work – the timer never fired. Your article fixed the problem. Again, many thanks.

  • Jeroen

    Thanks, but you forgot something. This code will only run the code inside the Timer1_Elapsed once… if you want to have the code run every 5000 miliseconds instead of only once you should at:

    Timer1.AutoReset = True;

    This resets the timer everytime the elapsed event fires.

  • remithr

    AutoReset is set to true by default

  • http://Elapsed Raj

    Timer1.elapsed is not in list

  • http://Elapsed Raj

    Even though i put autoreset true it run twice only. Once at the time service starts and second time the time elapsed first time. After that no activity

  • Bill Edward Adams

    I couldn’t get this to work until I changed the event args in the timer1_elapsed method to
    System.Timers.ElapsedEventArgs e

  • It

    Great code !

    Thanks.

  • TJ

    Thank you so much for this article. I couldn’t figure out why my service was not working! It turned out to be the Timer issue exactly as you described it. I followed your instructions, and presto! it works!

  • Jl

    Much appreciated. Awesome tutorial on how to implement the timer

  • Shaila

    Awesome Man!!

  • Leanhtoan2008

    Timer doesn’t need “timer.start()” to start.

  • Davepye800

     excellent article, well done Sir, well done indeed.

  • Abhishek

    Thanks a lot ……………….. It solves my issues very well…………

  • NiceWebTools.com

    Thank you it’s great blog,

  • AvasquezNI

    Excellent!! quite simple and efficient, greetings from Nicaragua :)

  • Nada

    Very nice! Thanks!
    Helped me a lot.

  • Codah FootPath

    cheerz… simple code but effective

  • Richard

    Been scratching my head on why my timer wasn’t firing for ages. This has got me working. Thanks.

  • Shivkumar DESHMUKH

    thanks a lot !!!

  • Prabhu

    Simple and Best.. Thanks.

  • Tuansusu

    Thanks

  • Hanzdolo

    It didn’t  work like that for me I dunno why. This is how it worked for me:
    using System.Timers;
    ..
    ..Timer timer1;..

    timer1= new Timer(5000);
    timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
    timer1.Enabled = true;
    timer1.Autoreset = true;timer1.Start();….
    private void timer1_Elapsed(object sender, ElapsedEventArgs e)
    {
          //Some awesome code!
    }

  • Montoto

    Awesome!!! Thanks… I started using Standard timer and I started making strange things like commenting or changing privileges on my Model because it was working while debugging but not on production. Thank you!!!

  • Raghu

    Thank you. This was very helpful