Change timer intervall in windows service
Posted
by
AyKarsi
on Stack Overflow
See other posts from Stack Overflow
or by AyKarsi
Published on 2011-01-12T14:38:16Z
Indexed on
2011/01/12
14:53 UTC
Read the original article
Hit count: 161
I have timer job inside a windows service, for which the intervall should be incremented when errors occur. My problem is that I can't get the timer.Change Method to actually change the intervall. The "DoSomething" is always called after the inital interval..
This is probably something simple ..
Code follows:
protected override void OnStart(string[] args)
{
//job = new CronJob();
timerDelegate = new TimerCallback(DoSomething);
seconds = secondsDefault;
stateTimer = new Timer(timerDelegate, null, 0, seconds * 1000);
}
public void DoSomething(object stateObject)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateObject;
if(!Busker.BitCoinData.Helpers.BitCoinHelper.BitCoinsServiceIsUp())
{
secondsDefault += secondsIncrementError;
if (seconds >= secondesMaximum)
seconds = secondesMaximum;
Loggy.AddError("BitcoinService not available. Incrementing timer to " +
secondsDefault + " s",null);
stateTimer.Change(seconds * 100, seconds * 100);
return;
}
else if (seconds > secondsDefault)
{
// reset the timer interval if the bitcoin service is back up...
seconds = secondsDefault;
Loggy.Add ("BitcoinService timer increment has been reset to " +
secondsDefault + " s");
}
// do the the actual processing here
}
© Stack Overflow or respective owner