C#中的三种定时计时器Timer⽤法介绍在.NET中有三种计时器:1、System.Windows.Forms命名空间下的Timer控件,它直接继承⾃Componet。
Timer控件只有绑定了Tick事件和设置Enabled=True后才会⾃动计时,停⽌计时可以⽤Stop()⽅法控制,通过Stop()停⽌之后,如果想重新计时,可以⽤Start()⽅法来启动计时器。
Timer控件和它所在的Form属于同⼀个线程;2、System.Timers命名空间下的Timer类。
System.Timers.Timer类:定义⼀个System.Timers.Timer对象,然后绑定Elapsed事件,通过Start()⽅法来启动计时,通过Stop()⽅法或者Enable=false停⽌计时。
AutoReset属性设置是否重复计时(设置为false只执⾏⼀次,设置为true可以多次执⾏)。
Elapsed事件绑定相当于另开了⼀个线程,也就是说在Elapsed绑定的事件⾥不能访问其它线程⾥的控件(需要定义委托,通过Invoke调⽤委托访问其它线程⾥⾯的控件)。
3、System.Threading.Timer类。
定义该类时,通过构造函数进⾏初始化。
在上⾯所述的三种计时器中,第⼀种计时器和它所在的Form处于同⼀个线程,因此执⾏的效率不⾼;⽽第⼆种和第三种计时器执⾏的⽅法都是新开⼀个线程,所以执⾏效率⽐第⼀种计时器要好,因此在选择计时器时,建议使⽤第⼆种和第三种。
下⾯是三种定时器使⽤的例⼦:1、Timer控件设计界⾯:后台代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace TimerDemo{public partial class FrmMain : Form{//定义全局变量public int currentCount = 0;public FrmMain(){InitializeComponent();}private void FrmMain_Load(object sender, EventArgs e){//设置Timer控件可⽤this.timer.Enabled = true;//设置时间间隔(毫秒为单位)this.timer.Interval = 1000;}private void timer_Tick(object sender, EventArgs e){currentCount += 1;this.txt_Count.Text = currentCount.ToString().Trim();}private void btn_Start_Click(object sender, EventArgs e) {//开始计时this.timer.Start();}private void btn_Stop_Click(object sender, EventArgs e) {//停⽌计时this.timer.Stop();}}}2、System.Timers.Timer设计界⾯:后台代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace TimersTimer{public partial class FrmMain : Form{//定义全局变量public int currentCount = 0;//定义Timer类System.Timers.Timer timer;//定义委托public delegate void SetControlValue(string value);public FrmMain(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e) {InitTimer();}/// <summary>/// 初始化Timer控件/// </summary>private void InitTimer(){//设置定时间隔(毫秒为单位)int interval = 1000;timer = new System.Timers.Timer(interval);//设置执⾏⼀次(false)还是⼀直执⾏(true)timer.AutoReset = true;//设置是否执⾏System.Timers.Timer.Elapsed事件timer.Enabled = true;//绑定Elapsed事件timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);}/// <summary>/// Timer类执⾏定时到点事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void TimerUp(object sender, System.Timers.ElapsedEventArgs e){try{currentCount += 1;this.Invoke(new SetControlValue(SetTextBoxText),currentCount.ToString()); }catch (Exception ex){MessageBox.Show("执⾏定时到点事件失败:" + ex.Message);}}/// <summary>/// 设置⽂本框的值/// </summary>/// <param name="strValue"></param>private void SetTextBoxText(string strValue){this.txt_Count.Text = this.currentCount.ToString().Trim();}private void btn_Start_Click(object sender, EventArgs e){timer.Start();}private void btn_Stop_Click(object sender, EventArgs e){timer.Stop();}}}3、System.Threading.Timer设计界⾯:后台代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;namespace Threading.Timer{public partial class FrmMain : Form{//定义全局变量public int currentCount = 0;//定义Timer类System.Threading.Timer threadTimer;//定义委托public delegate void SetControlValue(object value);public FrmMain(){InitializeComponent();}private void FrmMain_Load(object sender, EventArgs e){InitTimer();}/// <summary>/// 初始化Timer类/// </summary>private void InitTimer(){threadTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, 1000);}/// <summary>/// 定时到点执⾏的事件/// </summary>/// <param name="value"></param>private void TimerUp(object value){currentCount += 1;this.Invoke(new SetControlValue(SetTextBoxValue), currentCount);}/// <summary>/// 给⽂本框赋值/// </summary>/// <param name="value"></param>private void SetTextBoxValue(object value){this.txt_Count.Text = value.ToString();}/// <summary>/// 开始/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btn_Start_Click(object sender, EventArgs e){//⽴即开始计时,时间间隔1000毫秒threadTimer.Change(0, 1000);}/// <summary>/// 停⽌/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btn_Stop_Click(object sender, EventArgs e){//停⽌计时threadTimer.Change(Timeout.Infinite, 1000);}}}代码下载链接:到此这篇关于C#中的三种定时计时器Timer⽤法的⽂章就介绍到这了。