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 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:From the documentation: