开发者

C# Windows Service Timers not Firing

I am using a System.Threading.Timer in Windows service to execute a method periodically. The timer fires only once, it does not fire. Can anyone please help.

Below is the code

private TimerCallback timerDelegate;

protected override void开发者_如何学Go OnStart(string[] args)
{
    timerDelegate = new TimerCallback(DoWork);
    serviceTimer =  new Timer(timerDelegate, null, 1000,Timeout.Infinite);
}

private void DoWork(object state)
{
    GetMessages();
}


The final parameter (Timeout.Infinite) in your function call tells it to use an infinite interval between the first time it fires and each subsequent time. So it'll fire once after 1 second and then never again. You should probably do:

serviceTimer = new Timer(timerDelegate, null, 1000, 1000);

From the documentation:

The time interval between invocations of callback, in milliseconds. Specify Timeout.Infinite to disable periodic signaling.


The first timeout value is how long to wait for the first invocation of the callback, but the second is for repeat interval. You can set it to wait 5 minutes for the first "tick", and repeat every minute afterwards, for example.

By passing Timeout.Infinite as the last argument, you told it to only invoke once.

MSDN: System.Threading.Timer constructor

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜