共用方式為


HOW TO:使用 Windows Form Timer 元件以設定的間隔執行程序

更新:2007 年 11 月

有時候會想要建立一個按照指定時間間隔執行的程序,一直到迴圈完成或設定的時間間隔耗用完畢才停止。Timer 元件可用來設定這樣的程序。

這個元件是專為 Windows Form 環境而設計的。如需適用於伺服器環境的計時器,請參閱伺服器端計時器簡介

注意事項:

使用 Timer 元件時有一些限制。如需詳細資訊,請參閱 Windows Form Timer 元件的 Interval 屬性限制

若要使用 Timer 元件以設定的間隔執行程序

  1. Timer 加入表單中。請參閱下列的「範例」一節,以便取得以程式設計方式完成這項工作的實例。Visual Studio 同時也支援在表單中加入元件。

  2. 設定計時器的 Interval 屬性 (以毫秒為單位)。這個屬性決定程序再次執行前將經過多少時間。

    注意事項:

    計時器事件發生得越頻繁,用於回應事件的處理器時間也就越長。而這將降低整體的效能。請勿將間隔設定為比所需的時間短。

  3. Tick 事件處理常式中撰寫適當的程式碼。在這個事件中撰寫的程式碼,將以 Interval 屬性所指定的間隔執行。

  4. Enabled 屬性設為 true,以啟動計時器。Tick 事件將開始發生,並且以設定的間隔執行程序。

  5. 為了避免程序再次執行,請在適當的時間將 Enabled 屬性設為 false 以停止程序。將間隔設為 0 並不會使計時器停止。

範例

第一個程式碼範例是以每次遞增一秒的方式來追蹤每天的時間。它使用了表單上的 ButtonLabelTimer 元件。Interval 屬性是設為 1000 (相當於一秒)。在 Tick 事件中,標籤的標題是設為目前的時間。按下按鈕後,Enabled 屬性將設定為 false,以防止計時器更新標籤的標題。下列程式碼範例要求表單必須有名為 Button1 的 Button 控制項、 Timer1 的 Timer 控制項和 Label1 的 Label 按制項。

Private Sub InitializeTimer()
   ' Run this procedure in an appropriate event.
   ' Set to 1 second.
   Timer1.Interval = 1000
   ' Enable timer.
   Timer1.Enabled = True
   Button1.Text = "Enabled"
End Sub
x
Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
' Set the caption to the current time.
   Label1.Text = DateTime.Now
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      If Button1.Text = "Stop" Then
         Button1.Text = "Start"
         Timer1.Enabled = False
      Else
         Button1.Text = "Stop"
         Timer1.Enabled = True
      End If
End Sub
private void InitializeTimer()
{
   //' Run this procedure in an appropriate event.
   // Set to 1 second.
   Timer1.Interval = 1000;
   // Enable timer.
   Timer1.Enabled = true;
   Button1.Text = "Stop";
}

private void Timer1_Tick(object Sender, EventArgs e)   
{
   // Set the caption to the current time.
   Label1.Text = DateTime.Now.ToString();
}

private void Button1_Click()
{
  if ( Button1.Text == "Stop" )
  {
    Button1.Text = "Start";
    Timer1.Enabled = false;
  }
  else
  {
    Button1.Text = "Stop";
    Timer1.Enabled = true;
  }
}
private void InitializeTimer() 
{
   // Run this procedure in an appropriate event.
   // Set to 1 second.
   Timer1.set_Interval(1000);
   // Enable timer.
   Timer1.set_Enabled(true);
   Button1.set_Text("Stop");
}

private void Timer1_Tick(System.Object Sender, EventArgs e) 
{
   // Set the caption to the current time.
   Label1.set_Text(DateTime.get_Now().ToString());
}

private void Button1_Click() 
{
   if ( Button1.get_Text() == "Stop"  ) 
   {
      Button1.set_Text("Start");
      Timer1.set_Enabled(false);
   }
   else
   {
      Button1.set_Text("Stop");
      Timer1.set_Enabled(true);
   }
}
private:
   void InitializeTimer()
   {
      // Run this procedure in an appropriate event.
      // Set to 1 second.
      timer1->Interval = 1000;
      // Enable timer.
      timer1->Enabled = true;
      button1->Text = S"Stop";
   }

   void timer1_Tick(System::Object ^ sender,
      System::EventArgs ^ e)
   {
      // Set the caption to the current time.
      label1->Text = DateTime::Now.ToString();
   }

   void button1_Click(System::Object ^ sender,
      System::EventArgs ^ e)
   {
      if ( button1->Text == "Stop" )
      {
         button1->Text = "Start";
         timer1->Enabled = false;
      }
      else
      {
         button1->Text = "Stop";
         timer1->Enabled = true;
      }
   }

第二個程式碼範例會每隔 600 毫秒執行一個程序,直到迴圈完成為止。下列程式碼範例要求表單必須有名為 Button1 的 Button 控制項、 Timer1 的 Timer 控制項和 Label1 的 Label 按制項。

' This variable will be the loop counter.
Private counter As Integer

Private Sub InitializeTimer()
   ' Run this procedure in an appropriate event.
   counter = 0
   Timer1.Interval = 600
   Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
   If counter => 10 Then
      ' Exit loop code.
      Timer1.Enabled = False
      counter = 0
   Else
      ' Run your procedure here.
      ' Increment counter.
      counter = counter + 1
      Label1.Text = "Procedures Run: " & counter.ToString
   End If
End Sub
// This variable will be the loop counter.
private int counter;

private void InitializeTimer()
{
   // Run this procedure in an appropriate event.
   counter = 0;
   timer1.Interval = 600;
   timer1.Enabled = true;
   // Hook up timer's tick event handler.
   this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}

private void timer1_Tick(object sender, System.EventArgs e)   
{
   if (counter >= 10) 
   {
      // Exit loop code.
      timer1.Enabled = false;
      counter = 0;
   }
   else
   {
      // Run your procedure here.
      // Increment counter.
      counter = counter + 1;
      label1.Text = "Procedures Run: " + counter.ToString();
      }
}
// Run this procedure in an appropriate event.
counter = 0;
timer1.set_Interval(600);
timer1.set_Enabled(true);
// Wire up timer's tick event handler.
this.timer1.add_Tick(new System.EventHandler(this.timer1_Tick));

private void timer1_Tick(System.Object sender, System.EventArgs e) 
{
   if ( counter >= 10  ) 
   {
      // Exit loop code.
      timer1.set_Enabled(false);
      counter = 0;
   }
   else
   {
      // Run your procedure here.
      // Increment counter.
      counter = counter + 1;
      this.timer1.add_Tick(new System.EventHandler(this.timer1_Tick));
   }
}
private:
   int counter;

   void InitializeTimer()
   {
      // Run this procedure in an appropriate event.
      counter = 0;
      timer1->Interval = 600;
      timer1->Enabled = true;
      // Hook up timer's tick event handler.
      this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
   }

   void timer1_Tick(System::Object ^ sender,
      System::EventArgs ^ e)
   {
      if (counter >= 10) 
      {
         // Exit loop code.
         timer1->Enabled = false;
         counter = 0;
      }
      else
      {
         // Run your procedure here.
         // Increment counter.
         counter = counter + 1;
         label1->Text = String::Concat("Procedures Run: ",
            counter.ToString());
      }
   }

請參閱

參考

Timer 元件概觀 (Windows Form)

Timer

其他資源

Timer 元件 (Windows Form)