0
我有一個Timer
,我每10秒運行一次,以將我的應用程序中的本地數據同步到我們的服務器。定時器 - 不要重新運行,直到前一個完成
public Timer syncTimer;
syncTimer = new Timer((o) =>
{
this.syncToServer();
}, null, 0, 10000);
我有一個問題,如果我的syncToServer需要超過10秒鐘執行,它會自行跳過。我正在考慮做如下暫停計時器,而它執行,然後重新啓動一次完成:
public Timer syncTimer;
syncTimer = new Timer((o) =>
{
syncTimer.Change(Timeout.Infinite , Timeout.Infinite); //Stop the timer
this.syncToServer();
syncTimer.Change(10000 , 10000); //Restart the timer
}, null, 0, 10000);
這是實現我追求的還是有更好的方法來阻止它在執行的最佳方式同時?
如何設置布爾標誌?如果它現在正在執行(所以'boolean'爲true,只需跳過一次執行並在10秒內等待下一個執行? – Herdo 2014-09-28 13:46:25
這是http://stackoverflow.com/questions/1699543/stopping-timer-在其回調方法中?rq = 1 – 2014-09-28 14:11:01
對此使用'lock'塊 – 2014-09-28 14:21:40