2010-03-09 68 views
0

我的C#應用​​程序正在使用一個計時器來確定是否在及時發生預期事件。這是我當前如何試圖做到這一點:C#中的計時器事件,增量和復位

// At some point in the application where the triggering event has just occured. 
    // Now, the expected event should happen within the next second. 
    timeout = false; 
    timer1.Interval = 1000; // Set timeout for 1 second. 
    timer1.Start(); 

timer1_Tick(object sender, EventArgs e) 
{ 
    timeout = true; 
} 

// At some point in the application where the expected event has occured. 
    timer1.Stop(); 

// At a later point in the application where the timeout is 
// checked, before procedding. 
    if (timeout) 
    { 
     // Do something. 
    } 

現在,我想知道是被稱爲Start()Stop()成員方法時,不會導致定時器計數復位?我正在使用Microsoft Visual C#2008速成版。謝謝。

回答

3

當你調用Stop()它有效地復位定時器回到0,從鏈接的頁面:已禁用 後

調用啓動一個定時器,通過調用停止會造成 定時器啓動中斷的 間隔。如果您的定時器設置爲1000毫秒的間隔 ,並且您的 呼叫停止在大約3000毫秒,則呼叫啓動 將導致定時器 在 提高Tick事件之前等待5000毫秒。

+0

謝謝。我確實看過MSDN文章,但我一定錯過了這些信息。 – 2010-03-09 20:43:48

0

MSDN

調用start已通過調用停止將導致定時器重啓被中斷的間隔禁用的計時器之後。如果您的計時器設置爲5000毫秒間隔,並且在3000毫秒左右的時間內調用停止,則調用開始將導致計時器等待5000毫秒,然後引發滴答事件。