有时,你可能想要创建一个在特定时间间隔运行的过程,直到循环完成,或者在设置的时间间隔过后运行。 该 Timer 组件使此类过程成为可能。
此组件专为 Windows 窗体环境设计。 如果需要适合服务器环境的计时器,请参阅 Server-Based 计时器简介。
注释
使用 Timer 组件时存在一些限制。 有关详细信息,请参阅 Windows 窗体计时器组件的间隔属性的限制。
使用计时器组件在设定间隔执行程序
向表单添加Timer。 请参阅以下示例部分,了解如何以编程方式执行此作。 Visual Studio 还支持将组件添加到窗体。 另请参阅 如何:向 Windows 窗体添加没有用户界面的控件。
设置 Interval 计时器的属性(以毫秒为单位)。 此属性确定在再次运行过程之前将经过多少时间。
注释
计时器事件发生的频率越多,在响应事件时使用处理器时间就越长。 这可以降低整体性能。 不要设置比所需的更小的间隔。
在适当的时间,将 Enabled 属性设置为
false
停止过程再次运行。 将间隔0
设置为不会导致计时器停止。
第一个代码示例
第一个代码示例以一秒的增量跟踪一天的时间。 它在窗体上使用一个 Button 组件、一个 Label 组件和一个 Timer 组件。 该 Interval 属性设置为 1000(等于 1 秒)。 在Tick该事件中,标签的标题设置为当前时间。 单击按钮时,该 Enabled 属性设置为 false
停止计时器更新标签的标题。 下面的代码示例要求你有一个包含名为Button的控件、名为Timer的控件和名为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();
}
}
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());
}
}