定时器控制间隔
我在我的C#.net项目中使用了一个定时器控件,定时器间隔是5秒。 在timer_tick方法中,我调用了另一个计算方法,需要5分钟才能完成。 我的问题是timer_tick方法将每5秒调用一次,还是会等待上一个进程完成? 在我的测试中,我在调用计算方法之前将当前时间写入输出窗口。 所以,我发现它总是在等待上一个进程完成。如果是正确的,那么,计时器间隔是做什么用的? 我的测试代码是这样的,
private void timer1_Tick(object sender, EventArgs e)
{
Console.WriteLine(DateTime.Now.ToString());
CalculationMethod();
}
问候,
Indi
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这完全取决于您使用的计时器类型!以下文章提供了 3 个框架计时器的行为的全面指南:
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx" rel="nofollow">http:// /msdn.microsoft.com/en-us/magazine/cc164015.aspx
有些会保证持续的滴答声(尽其所能),其他人会跳过或暂停他们的滴答声。
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.
Tick 事件对 timer1_Tick 方法进行阻塞(同步)调用,因此如果方法需要很长时间,它将等待。它适用于可以在给定时间内完成的事情。
如果您确实需要每 5 秒调用一次此方法,请每次都为它们生成新线程:
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:
它会等待,因为在
CalculationMethod
返回之前timer1_Tick
无法返回。如果您不想等待CalculationMethod
,您应该将其实现为异步调用(例如在后台工作)。It waits because
timer1_Tick
can't return beforeCalculationMethod
returns. If you want to not to wait forCalculationMethod
you should implement it as asynchronous call (for example to work in background).查看此 MSDN 页面 上的注释。计时器控件是单线程的,因此它将等待第一个事件完成。您应该查看 Timers 类
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