我在Windows服務中有計時器作業,發生錯誤時應該爲其增加間隔。我的問題是我無法獲得timer.Change方法來實際更改間隔。 「DoSomething」始終在初始間隔後調用。在Windows服務中更改計時器間隔
代碼如下:
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
}
傻我。那很簡單。謝謝! – AyKarsi 2011-01-12 15:01:49