[英]How to check is timer enabled/disabled?
我嘗試了以下方法,但對我不起作用。
if (MyTimer.Enabled)
{
continue;
}
else
{
break;
}
如果計時器結束,我想中斷for
循環。
您可以按照以下說明中斷循環。 定時器時間一過,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.