简体   繁体   English

如何延迟计时器正确启动?

[英]How to do a delay of timer start properly?

Using Winforms, I need to delay execution of event timer1_Tick itself or may delay timer1.Start() as well. 使用Winforms,我需要延迟事件timer1_Tick本身的执行,或者也可能延迟timer1.Start()

I have no idea how to do that properly. 我不知道该怎么做。 Could someone give an advice? 有人可以给个建议吗?

Just put Thread.Sleep before handle event 只需将Thread.Sleep放在句柄事件之前

public void Timer1_tick(...){
   System.Threading.Thread.Sleep(1000);
}

or if you need to delay timer1.Start() execution 或者如果需要延迟timer1.Start()执行

System.Threading.Thread.Sleep(1000);
timer1.Start();

1.) Use another timer ("helper") that starts the intended timer at its first tick, then stop this helper timer. 1.)使用另一个计时器(“帮助程序”),该计时器在其第一次计时时启动所需的计时器,然后停止该帮助程序计时器。 2.) Cout the ticks of your timer and only execute the event handler code after the tick count exceeded a limit: 2.)计时计时器的滴答声,并且只有在滴答声计数超过限制后才执行事件处理程序代码:

This example starts the event handler code only after the first 100 ticks: 本示例仅在前100个滴答之后启动事件处理程序代码:

int tickCount;
public void Timer1_tick(...)
{
   if( tickCount++ < 100 ) return;

   // Do something...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM