我正在使用兩個System.Threading.Timer實例來關閉兩個週期性重複的任務。正在進行回調時禁用System.Threading.Timer實例
我的問題是:如果定時器被禁止,但在那個時間點,該計時器上執行一個線程它的回調,那麼Main方法將退出或者它會等待執行回調來完成?
在下面的代碼中,Method1RunCount使用鎖定語句進行讀取和寫入同步(這部分代碼未顯示在下面)。 timer1的回調會在每次運行結束時將Method1RunCount遞增1。
static void Main(string[] args)
{
TimerCallback callback1 = Method1;
System.Threading.Timer timer1 = new System.Threading.Timer(callback1,null,0, 90000);
TimerCallback callback2 = Method2;
System.Threading.Timer timer2 = new System.Threading.Timer(callback2, null, 0, 60000);
while (true)
{
System.Threading.Thread.Sleep(250);
if (Method1RunCount == 4)
{
//DISABLE the TIMERS
timer1.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
timer2.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
break;
}
}
}