Timer 组件

使用计时器组件以设置的间隔运行过程

  1. 在窗体中添加 Timer。有关如何以编程方式执行此操作的说明,请参见以下示例部分。此外,Visual Studio 还支持在窗体中添加组件。 有关更多信息,请参见如何:向 Windows 窗体添加无用户界面的控件

  2. 为计时器设置 Interval 属性(以毫秒为单位)。该属性决定在再次运行该过程之前所经过的时间。

    Note注意

    计时器事件发生越频繁,用于响应该事件的处理器时间就越长。这会降低整体性能。请勿将间隔设置得比所需值小。

  3. Tick 事件处理程序内编写合适的代码。在该事件中编写的代码将以 Interval 属性中所指定的间隔运行。

  4. Enabled 属性设置为 true,以启动计时器。Tick 事件将开始发生,从而以设置的间隔运行过程。

  5. 合适的时候,将 Enabled 属性设置为 false,以使过程停止再次运行。将间隔设置为 0,并不会导致计时器停止。

示例

第一个代码示例以一秒为增量单位跟踪每天的时间。它在窗体中使用了 ButtonLabel 和 Timer 组件。将 Interval 属性设置为 1000(等于一秒钟)。在 Tick 事件中,将标签的标题设置为当前时间。当单击按钮时,Enabled 属性将设置为 false,以使计时器停止更新标签的标题。下面的代码示例要求您拥有一个窗体,该窗体具有一个名为 Button1 的 Button 控件、一个名为 Timer1 的 Timer 控件和一个名为 Label1 的 Label 控件。

Visual Basic  CopyCode image复制代码
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

C#  CopyCode image复制代码
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;
  }
}
J#  CopyCode image复制代码
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);
   }
}
C++  CopyCode image复制代码
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 控件。

Visual Basic  CopyCode image复制代码
' 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

C#  CopyCode image复制代码
// 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();
      }
}

Timer控件

Timer控件主要会用到2个属性一个是Enabled和Interval
Enabled主要是控制当前Timer控件是否可用
timer1.Enabled=false;不可用
timer1.Enabled=true;可用
timer1.Interval=1000;主要是设置timer2_Tick事件的时间,单位为毫秒

例一:到9:00提示去上厕所:(
把timer2.Interval=60000;//1分钟


private void timer2_Tick(object sender, System.EventArgs e){
        string cesuotime=DateTime.Now.DateTime.Now.ToShortTimeString();//得到现在的时间
         if(cesuotime.equles("9:00")){
                       timer1.Enabled=false;
                       MessageBox.show("该去上厕所了");
                       timer1.Enabled=true;//如果不先把enabled设置成false对话框会一直弹下去
           }
}


例二:每2小时提示用户看电脑时间已经很久了,需要休息了
把timer2.Interval=7200000;//7200秒


private void timer2_Tick(object sender, System.EventArgs e){
                        timer1.Enabled=false;
                       MessageBox.show("需要休息了,开机已经2小时了");
                       timer1.Enabled=true;//如果不先把enabled设置成false对话框会一直弹下去
           }
}

posted on 2007-05-10 12:50  石川  阅读(388)  评论(0编辑  收藏  举报