有时您可能想创建一个过程,使其以特定时间间隔运行直至一个循环完成,或者使其在经过所设置的时间间隔后运行。 Timer 组件使这种过程成为可能。
该组件是为 Windows 窗体环境设计的。 如果您需要适合服务器环境的计时器,请参见 Introduction to Server-Based Timers。
提示
使用 Timer 组件时会受到某些限制。 有关更多信息,请参见 Windows 窗体 Timer 组件的 Interval 属性的限制。
使用计时器组件以设置的间隔运行过程
- 在窗体中添加 Timer。 有关如何以编程方式执行此操作的说明,请参见以下示例部分。 此外,Visual Studio 还支持在窗体中添加组件。 有关更多信息,请参见 如何:向 Windows 窗体添加无用户界面的控件 和 如何:向 Windows 窗体添加无用户界面的控件 和 如何:向 Windows 窗体添加无用户界面的控件 和 如何:向 Windows 窗体添加无用户界面的控件. 
- 为计时器设置 Interval 属性(以毫秒为单位)。 该属性决定在再次运行该过程之前所经过的时间。 - 提示 - 计时器事件发生越频繁,用于响应该事件的处理器时间就越长。 这会降低整体性能。 请勿将间隔设置得比所需值小。 
- 合适的时候,将 Enabled 属性设置为 false,以使过程停止再次运行。 将间隔设置为 0,并不会导致计时器停止。 
示例
第一个代码示例以一秒为增量单位跟踪每天的时间。 它在窗体中使用了 Button、Label 和 Timer 组件。 将 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()
{
    // Call this procedure when the application starts.
    // Set to 1 second.
    Timer1.Interval = 1000;
    Timer1.Tick += new EventHandler(Timer1_Tick);
    // Enable timer.
    Timer1.Enabled = true;
    Button1.Text = "Stop";
    Button1.Click += new EventHandler(Button1_Click);
}
private void Timer1_Tick(object Sender, EventArgs e)   
{
   // Set the caption to the current time.
   Label1.Text = DateTime.Now.ToString();
}
private void Button1_Click(object sender, EventArgs e)
{
  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->Interval = 1000;
      // Enable timer.
      timer1->Enabled = true;
      this->timer1->Tick += gcnew System::EventHandler(this,  
                               &Form1::timer1_Tick);
      button1->Text = S"Stop";
      this->button1->Click += gcnew System::EventHandler(this, 
                               &Form1::button1_Click);
   }
   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());
      }
   }