一、System.Threading.Timer 与System.Windows.Forms.Timer
System.Threading.Timer 是一个简单的轻量计时器,它使用回调方法并由线程池线程提供服务。不建议将其用于 Windows 窗体,因为其回调不在用户界面线程上进行(硬要使用的话只能通过委托的方式进行操作界面元素处理)。
System.Windows.Forms.Timer 是用于 Windows 窗体的更佳选择。要获取基于服务器的计时器功能,可以考虑使用 System.Timers.Timer,它可以引发事件并具有其他功能。
二、案例测试说明
1、现需要在界面实现一个倒计时功能。效果如下:
案例代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
-
- namespace CountDownCase
- {
- public partial class Form1 : Form
- {
- public delegate void DelegateSetControls(Object state);
- public int timeCount = 10;
- public System.Windows.Forms.Timer timer2;
-
- public Form1()
- {
- InitializeComponent();
- timer2 = new System.Windows.Forms.Timer();
- /* This sentence can't be used many times. */
- timer2.Tick += new EventHandler(TimerEventProcessor);
- }
-
- /*********************---------------------------------------------------------------------------------------------------------------
- * Related operation of timer1
- *-------------------------------------------------------------------------------------------------------------------*****************/
- private void timer1_Tick(object sender, EventArgs e)
- {
- this.label1.Text = "CountDown Beginning......";
- if (timeCount != 0)
- {
- this.label2.Text = timeCount.ToString() + "Second";
- timeCount--;
- }
- else
- {
- this.label2.Text = "0 Second";
- this.label1.Text = "The countdown is over......";
- timer1.Stop();
- }
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- timeCount = 10;
- timer1.Start();
- }
-
- /*********************------------------------------------------------------------------------------------------------------------------
- * related operation of timer2
- *-------------------------------------------------------------------------------------------------------------------********************/
- public int timeCountTimer2 = 10;
- private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
- {
- if (timeCountTimer2 != 0)
- {
- this.label3.Text = timeCountTimer2.ToString() + "second";
- timeCountTimer2--;
- }
- else
- {
- this.label3.Text = "0 second";
- this.label4.Text = "Ready for operation......";
- this.timer2.Stop();
- }
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- timeCountTimer2 = 10;
- this.timer2.Interval = 1000;
- this.timer2.Start();
- this.label4.Text = "CountDown has begun......";
- }
-
- }
- }

2、关键代码分析
窗体控件法使用Timer的步骤:
(1)、拖控件
(2)、设置Timer的几个属性
(3)、编写timer的Tick事件
- private void timer1_Tick(object sender, EventArgs e)
- {
- this.label1.Text = "CountDown Beginning......";
- if (timeCount != 0)
- {
- this.label2.Text = timeCount.ToString() + "Second";
- timeCount--;
- }
- else
- {
- this.label2.Text = "0 Second";
- this.label1.Text = "The countdown is over......";
- timer1.Stop();
- }
- }
代码法创建Timer的步骤麻烦些:
(1)、创建好对象
- timer2 = new System.Windows.Forms.Timer();
(2)、编写Tick事件委托函数并赋值
- timer2.Tick += new EventHandler(TimerEventProcessor);
-
- private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
- {
- if (timeCountTimer2 != 0)
- {
- this.label3.Text = timeCountTimer2.ToString() + "second";
- timeCountTimer2--;
- }
- else
- {
- this.label3.Text = "0 second";
- this.label4.Text = "Ready for operation......";
- this.timer2.Stop();
- }
- }
(3)、设置定时触发时钟的时间
- this.timer2.Interval = 1000;
- this.timer2.Start();
OVER!