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.
you can break your loop as written below. The while loop will break as soon as the timer time elapsed.
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;
}
Reference here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.