Ich habe ein Timer-Objekt. Ich möchte, dass es jede Minute ausgeführt wird. Insbesondere sollte es eine OnCallBackMethode ausführen und wird inaktiv, während eine OnCallBackMethode ausgeführt wird. Sobald eine OnCallBackMethode beendet ist, OnCallBackstartet sie (a ) einen Timer neu.
Folgendes habe ich gerade:
private static Timer timer;
private static void Main()
{
timer = new Timer(_ => OnCallBack(), null, 0, 1000 * 10); //every 10 seconds
Console.ReadLine();
}
private static void OnCallBack()
{
timer.Change(Timeout.Infinite, Timeout.Infinite); //stops the timer
Thread.Sleep(3000); //doing some long operation
timer.Change(0, 1000 * 10); //restarts the timer
}
Es scheint jedoch nicht zu funktionieren. Es läuft alle 3 Sekunden sehr schnell. Auch wenn Sie einen Zeitraum erhöhen (1000 * 10). Es scheint, als würde es ein Auge zudrücken1000 * 10
Was habe ich falsch gemacht?
Timer.Change: "Wenn dueTime Null (0) ist, wird die Rückrufmethode sofort aufgerufen." Sieht so aus, als wäre es Null für mich.