2014-10-28 46 views
0

我正在研究一個web應用程序。在這個應用程序中,我想在12分鐘後暫停計時器。我怎麼能這樣做?或者我可以在哪裏指定時間,以便計時器滴答停頓?如何在預定義時間後暫停Timer_Tick?

+0

你可以有後12分鐘停止第一定時器第二定時器。 – 2014-10-28 12:02:21

+0

幾分鐘後如何停止計時器? @Ben Robinson – 2014-10-28 12:02:39

+0

如何擁有持續時間= 12分鐘的另一個計時器?在它的勾號禁用舊的 – 2014-10-28 12:02:43

回答

0

您可以使用回調中的狀態對象來獲取計時器實例。
然後,如果您想停止計時器,則可以在回撥內查看。
您可以簡單地計算節拍數和計算所經過的時間在每一個刻度:

... 
    // Start the timer 
    // you have to use this constructor if you want to 
    // access the timer itself inside callback 
    var timer = new Timer(Callback); 
    timer.Change(100, period); 
} 

// Set desired period 
private static int period = 100; 

// Track elapsed time 
private static int elapsed; 

private static void Callback(object state) 
{ 
    // update elapsed time 
    ellapsed += period; 

    // check if ellapsed is greater than 12min 
    bool isElapsed = ellapsed >= 12 * 60 * 1000; 

    if (!isElapsed) return; 

    var timer = state as Timer; 

    if (timer != null) 
     timer.Change(Timeout.Infinite, Timeout.Infinite); 
} 
相關問題