[英]How to check is timer enabled/disabled?
I tried the below but it won't work for me. 我尝试了以下方法,但对我不起作用。
if (MyTimer.Enabled)
{
continue;
}
else
{
break;
}
I want to break the for
loop if the timer is over. 如果计时器结束,我想中断
for
循环。
you can break your loop as written below. 您可以按照以下说明中断循环。 The while loop will break as soon as the timer time elapsed.
定时器时间一过,while循环就会中断。
private static System.Timers.Timer MyTimer;
static bool runningloop;
public static void Main()
{
runningloop=true;
MyTimer = new System.Timers.Timer();
MyTimer.Interval = 2000;
MyTimer.Elapsed += OnTimedEvent;
MyTimer.Enabled = true;
// If the timer is declared in a long-running method, use KeepAlive to prevent garbage collection
// from occurring before the method ends.
//GC.KeepAlive(MyTimer)
while(runningloop)
{
//do your work here
}
}
private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
runningloop=false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.