Timer Control Interval
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 previ开发者_如何学Pythonous 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
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.
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());
}
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).
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
精彩评论