共用方式為


Timer 類別

定義

實作一個計時器,在使用者定義的間隔內觸發事件。 此計時器為 Windows 表單應用程式優化,必須在視窗中使用。

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

範例

以下範例實作了一個簡單的間隔計時器,每五秒觸發一次警報。 警報發生時,A 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 )
      {
         
         // 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) {
          // 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

備註

A Timer 用於在使用者定義的間隔內觸發事件。 這個 Windows 計時器是為單執行緒環境設計,使用介面執行緒來執行處理。 這需要使用者程式碼有 UI 訊息泵,並且始終從同一個執行緒操作,或將呼叫管理到另一個執行緒。

使用這個計時器時,請利用事件 Tick 執行輪詢操作或顯示指定時間內的啟動畫面。 當屬性Enabled設定trueInterval為且屬性大於零時,Tick事件會根據屬性設定在間隔Interval內觸發。

此類別提供設定間隔及開始與停止計時器的方法。

備註

Windows 表單計時器元件為單執行緒,準確度限制為 55 毫秒。 如果你需要更精確的多執行緒計時器,請使用 Timer 命名空間中的 System.Timers 類別。

建構函式

名稱 Description
Timer()

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

Timer(IContainer)

初始化該類別的新實例 Timer 及指定的容器。

屬性

名稱 Description
CanRaiseEvents

會得到一個值,表示該元件是否能引發事件。

(繼承來源 Component)
Container

得到 IContainer 包含 Component的 。

(繼承來源 Component)
DesignMode

會得到一個值,表示目前 Component 是否處於設計模式。

(繼承來源 Component)
Enabled

讀取或設定計時器是否正在運行。

Events

會取得與此 Component連結的事件處理程序清單。

(繼承來源 Component)
Interval

取得或設定事件發生前 Tick 相對於事件最後發生 Tick 時間的時間(以毫秒為單位)。

Site

取得或設定 ISiteComponent

(繼承來源 Component)
Tag

取得或設定一個任意字串,代表某種使用者狀態。

方法

名稱 Description
CreateObjRef(Type)

建立一個物件,包含產生代理伺服器所需的所有相關資訊,用於與遠端物件通訊。

(繼承來源 MarshalByRefObject)
Dispose()

釋放所有由 Component.

(繼承來源 Component)
Dispose(Boolean)

處理計時器所使用的資源(記憶體除外)。

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

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

取得目前控制此實例生命週期政策的終身服務物件。

(繼承來源 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串,代表 。

事件

名稱 Description
Disposed

當元件被呼叫方法 Dispose() 時會發生。

(繼承來源 Component)
Tick

當指定的計時器間隔結束且計時器被啟用時,會發生這種情況。

適用於