2011-01-12 10 views
4

エラーが発生したときに間隔を増やす必要がある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 
} 

答えて

2

あなたの実際の問題は、この行である:

secondsDefault += secondsIncrementError; 

それは次のようになります。

seconds += secondsIncrementError; 

さらに、Timer.Change方法はそう、ミリ秒で動作100を掛けることは明らかに間違っています。それは変化を意味する:

stateTimer.Change(seconds * 1000, seconds * 1000); 

stateTimer.Change(seconds * 100, seconds * 100); 

はそれがお役に立てば幸いです。

+0

愚かな私。それは簡単です。ありがとうございました! – AyKarsi

0

stateTimer.Change(0, seconds * 100);を使用すると、すぐに新しい間隔でSystem.Threading.Timerが再開されます。

+0

それを試しましたが、それは同じ効果があります:( – AyKarsi