views:

265

answers:

5

If I have a Windows Service that needs to execute a task every 30 seconds which is better to use; the Timer() class or a loop that executes the task then sleeps for a number of seconds?

class MessageReceiver 
{
    public MessageReceiver()
    {
    }

    public void CommencePolling()
    {
     while (true)
     {
      try 
      {
       this.ExecuteTask();  
       System.Threading.Thread.Sleep(30000);
      }
      catch (Exception)
      {
       // log the exception
      }
     }
    }

    public void ExecutedTask()
    {
     // do stuff
    }
}

class MessageReceiver 
{
    public MessageReceiver()
    {
    }

    public void CommencePolling()
    {
     var timer = new Timer()
      {
       AutoReset = true,
       Interval = 30000,
       Enabled = true
      };

     timer.Elapsed += Timer_Tick;    
    }

    public void Timer_Tick(object sender, ElapsedEventArgs args)
    {
     try 
     {
      // do stuff
     }
     catch (Exception)
     {
      // log the exception
     }
    }
}

The windows service will create an instance of the MessageReciever class and execute the CommencePolling method on a new thread.

A: 

Are you doing anything else during that ten second wait? Using Thread.sleep would block, preventing you from doing other things. From a performance point of view I don't think you'd see too much difference, but I would avoid using Thread.sleep myself.

There are three timers to choose from - System.Windows.Forms.Timer is implemented on the main thread whereas System.Timers.Timer and System.Threading.Timer are creating seperate threads.

Sohnee
A: 

I believe both methods are equivalent. There will be a thread either way: either because you create one, or because the library implementing the Timer class creates one.

Using the Timer class might be slightly more less expensive resource-wise, since the thread implementing timers probably monitors other timeouts as well.

Martin v. Löwis
A: 

I this the answers to this question will help.

Chris Melinn
A: 

Not answered by me but John Saunders (above)... the answer can be found here http://stackoverflow.com/questions/1099516/for-a-windows-service-which-is-better-a-wait-spin-or-a-timer

Kane
+2  A: 

I think it really depends on your requirement.

case 1. Suppose you want to run this.ExecuteTask() every five minutes starting from 12:00AM (i.e., 12:00, 12:05, ...) and suppose the execution time of this.ExecuteTask() varies (for example, from 30 sec to 2 min), maybe using timer instead of Thread.Sleep() seems to be an easier way of doing it (at least for me).

However, you can achieve this behavior with Thread.Sleep() as well by calculating the offset while taking timestamps on a thread wake-up and on a completion of this.ExecuteTask().

case 2. Suppose you want to perform the task in the next 5 min just after completion of this.ExecuteTask(), using Thread.Sleep() seems to be easier. Again, you can achieve this behavior with a timer as well by reseting the timer every time while calculating offsets on every time this.ExecuteTask() completes.

Note1, for the case 1, you should be very careful in the following scenario: what if this.ExecuteTask() sometimes takes more than the period (i.e. it starts at 12:05 and completes 12:13 in the example above).

  1. What does this mean to your application and how will it be handled?

    a. Total failure - abort the service or abort the current(12:05) execution at 12:10 and launch 12:10 execution.
    b. Not a big deal (skip 12:10 one and run this.ExecuteTask() at 12:15).

    c. Not a big deal, but need to launch 12:10 execution immediately after 12:05 task finishes (what if it keeps taking more than 5 min??).

    d. Need to launch 12:10 execution even though 12:05 execution is currently running.

    e. anything else?

  2. For the policy you select above, does your choice of implementation (either timer or Thread.Sleep()) easy to support your policy?

Note2. There are several timers you can use in .NET. Please see the following document (even though it's bit aged, but it seems to be a good start): Comparing the Timer Classes in the .NET Framework Class Library

Chansik Im