Timer 類別

定義

實作一個在使用者定義的間隔引發事件的計時器。 最佳化這個計時器以用於 Windows Form 應用程式,並且必須用於視窗中。

public ref class Timer : System::ComponentModel::Component
public class Timer : System.ComponentModel.Component
type Timer = class
    inherit Component
Public Class Timer
Inherits Component
繼承

範例

下列範例會實作簡單的間隔計時器,每五秒會設定一次警示。 發生警示時,會顯示 MessageBox 警示已啟動的次數計數,並提示使用者是否應該繼續執行計時器。

public ref class Class1
{
private:
   static System::Windows::Forms::Timer^ myTimer = gcnew System::Windows::Forms::Timer;
   static int alarmCounter = 1;
   static bool exitFlag = false;

   // This is the method to run when the timer is raised.
   static void TimerEventProcessor( Object^ /*myObject*/, EventArgs^ /*myEventArgs*/ )
   {
      myTimer->Stop();
      
      // Displays a message box asking whether to continue running the timer.
      if ( MessageBox::Show( "Continue running?", String::Format( "Count is: {0}", alarmCounter ), MessageBoxButtons::YesNo ) == DialogResult::Yes )
      {
         
         // Restarts the timer and increments the counter.
         alarmCounter += 1;
         myTimer->Enabled = true;
      }
      else
      {
         
         // Stops the timer.
         exitFlag = true;
      }
   }


public:
   static void Main()
   {
      
      /* Adds the event and the event handler for the method that will 
                process the timer event to the timer. */
      myTimer->Tick += gcnew EventHandler( TimerEventProcessor );
      
      // Sets the timer interval to 5 seconds.
      myTimer->Interval = 5000;
      myTimer->Start();
      
      // Runs the timer, and raises the event.
      while ( exitFlag == false )
      {
         
         // Processes all the events in the queue.
         Application::DoEvents();
      }
   }

};

int main()
{
   Class1::Main();
}
public class Class1 {
    static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    static int alarmCounter = 1;
    static bool exitFlag = false;
 
    // This is the method to run when the timer is raised.
    private static void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs) {
       myTimer.Stop();
 
       // Displays a message box asking whether to continue running the timer.
       if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter, 
          MessageBoxButtons.YesNo) == DialogResult.Yes) {
          // Restarts the timer and increments the counter.
          alarmCounter +=1;
          myTimer.Enabled = true;
       }
       else {
          // Stops the timer.
          exitFlag = true;
       }
    }
 
    public static int Main() {
       /* Adds the event and the event handler for the method that will 
          process the timer event to the timer. */
       myTimer.Tick += new EventHandler(TimerEventProcessor);
 
       // Sets the timer interval to 5 seconds.
       myTimer.Interval = 5000;
       myTimer.Start();
 
       // Runs the timer, and raises the event.
       while(exitFlag == false) {
          // Processes all the events in the queue.
          Application.DoEvents();
       }
    return 0;
    }
 }
Public Class Class1
    Private Shared WithEvents myTimer As New System.Windows.Forms.Timer()
    Private Shared alarmCounter As Integer = 1
    Private Shared exitFlag As Boolean = False    
    
    ' This is the method to run when the timer is raised.
    Private Shared Sub TimerEventProcessor(myObject As Object, _
                                           ByVal myEventArgs As EventArgs) _
                                       Handles myTimer.Tick
        myTimer.Stop()
        
        ' Displays a message box asking whether to continue running the timer.
        If MessageBox.Show("Continue running?", "Count is: " & alarmCounter, _
                            MessageBoxButtons.YesNo) = DialogResult.Yes Then
            ' Restarts the timer and increments the counter.
            alarmCounter += 1
            myTimer.Enabled = True
        Else
            ' Stops the timer.
            exitFlag = True
        End If
    End Sub
    
    Public Shared Sub Main()
        ' Adds the event and the event handler for the method that will
        ' process the timer event to the timer.
        
        ' Sets the timer interval to 5 seconds.
        myTimer.Interval = 5000
        myTimer.Start()
        
        ' Runs the timer, and raises the event.
        While exitFlag = False
            ' Processes all the events in the queue.
            Application.DoEvents()
        End While

    End Sub    

End Class

備註

Timer用來在使用者定義的間隔引發事件。 此 Windows 計時器是專為使用 UI 執行緒來執行處理的單一線程環境所設計。 使用者程式碼需要有可用的 UI 訊息幫浦,且一律從相同的執行緒操作,或將呼叫封送處理至另一個執行緒。

當您使用此計時器時,請使用 Tick 事件來執行輪詢作業,或顯示指定時段的啟動顯示畫面。 每當 Enabled 屬性設定為 trueInterval 屬性大於零時,就會 Tick 根據 Interval 屬性設定以間隔引發事件。

這個類別提供方法來設定間隔,以及啟動和停止計時器。

注意

Windows Forms計時器元件是單一執行緒,而且限制為 55 毫秒的精確度。 如果您需要具有更高精確度的多執行緒計時器,請使用 Timer 命名空間中的 System.Timers 類別。

建構函式

Timer()

初始化 Timer 類別的新執行個體。

Timer(IContainer)

使用指定的容器,初始化 Timer 類別的新執行個體。

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
Container

取得包含 IContainerComponent

(繼承來源 Component)
DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
Enabled

取得或設定計時器是否正在執行。

Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
Interval

Tick 事件相對於上一次發生 Tick 事件而引發之前,取得或設定時間 (以毫秒為單位)。

Site

取得或設定 ComponentISite

(繼承來源 Component)
Tag

取得或設定表示部分類型使用者狀態的任意字串。

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

處置計時器所使用記憶體之外的資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
OnTick(EventArgs)

引發 Tick 事件。

Start()

啟動計時器。

Stop()

停止計時器。

ToString()

傳回表示 Timer 的字串。

事件

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)
Tick

發生於指定的計時器間隔已耗用,且計時器被啟用時。

適用於