1

I used one timer control in my C#.net project,timer interval is 5 second. In timer_tick method,i call another calculation method that will take 5 minute to complete. My question is timer_tick method will be call in every 5 second or it will wait previous process finish? In my testing,i write the current time to output window before calling calculation method. So,I found it always waiting for previous process finish.If it is correct,So,what is timer interval for? My testing code is like this,

private void timer1_Tick(object sender, EventArgs e)
{ 
    Console.WriteLine(DateTime.Now.ToString());
    CalculationMethod();
}

Regards,

Indi

Dulini Atapattu
  • 2,735
  • 8
  • 33
  • 47
Indi
  • 191
  • 1
  • 4
  • 13
  • 1
    Related post: http://stackoverflow.com/questions/1416803/system-timers-timer-vs-system-threading-timer – Dariusz May 19 '11 at 10:18

4 Answers4

4

It entirely depends on the type of timer you are using! The following article gives a comprehensive guide to the behaviour of the 3 framework timers:

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

Some will guarantee a constant tick (as best they can) others will skip, or pause their ticks.

ColinE
  • 68,894
  • 15
  • 164
  • 232
  • +1 that would have been my answer, too. The best article describing the pro and cons of the three available timer classes in the .net framework. – Oliver May 19 '11 at 10:16
2

The Tick event makes a blocking (synchronous) call to the timer1_Tick method so if method takes a very long time, it will wait. It is meant for things which can be completed in the given timeframe.

If you really need to call this method every 5 seconds, spawn new threads for them on each go:

private void timer1_Tick(object sender, EventArgs e)
{ 
    Console.WriteLine(DateTime.Now.ToString());
    Task.Factory.StartNew(() => CalculationMethod());
}
Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
1

It waits because timer1_Tick can't return before CalculationMethod returns. If you want to not to wait for CalculationMethod you should implement it as asynchronous call (for example to work in background).

Dariusz
  • 15,573
  • 9
  • 52
  • 68
1

Take a look at remarks on this MSDN page . The timer control is single thread so it will wait for the first event to complete. You should take a look at the Timers class

dfpados
  • 11
  • 1