繁体   English   中英

如何检查计时器是否启用?

[英]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.

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