0

在此链接中,它显示了如何确定计时器是否正在运行。对我来说,它不起作用。

我在静态类中声明了计时器,如图所示

public static class ServiceGlobals // Globals
{

    public static System.Timers.Timer _timer = new System.Timers.Timer();
}

}

在我的服务启动时,我设置了计时器属性

 protected override void OnStart(string[] args)
        {                  

                    ServiceGlobals._timer.AutoReset = false;
                    ServiceGlobals._timer.Interval = (3000);
                    ServiceGlobals._timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
                    ServiceGlobals._timer.Enabled = true;
                    ServiceGlobals._timer.Start(); // Start timer                  
        }

然后我检查它是否在我的一种方法中运行,但即使它正在运行,代码也总是错误的

if (ServiceGlobals._timer.Enabled) // Check if the timer is running
{
    // Return error.........

}
4

1 回答 1

2

您一定错过了您链接的线程的这一部分:

“如果 Timer.AutoReset 为 true,则 Enabled 将在计时器第一次到期时自动设置为 false。”

于 2014-04-13T16:25:41.700 回答