博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#里面的三种定时计时器:TIMER
阅读量:4350 次
发布时间:2019-06-07

本文共 6035 字,大约阅读时间需要 20 分钟。

在.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控件设计界面:后台代码:复制代码 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace TimerDemo11 {12     public partial class FrmMain : Form13     {14         //定义全局变量15         public int currentCount = 0;16         public FrmMain()17         {18             InitializeComponent();19         }20 21         private void FrmMain_Load(object sender, EventArgs e)22         {23             //设置Timer控件可用24             this.timer.Enabled = true;25             //设置时间间隔(毫秒为单位)26             this.timer.Interval = 1000;27         }28 29         private void timer_Tick(object sender, EventArgs e)30         {31             currentCount += 1;32             this.txt_Count.Text = currentCount.ToString().Trim();33         }34 35         private void btn_Start_Click(object sender, EventArgs e)36         {37             //开始计时38             this.timer.Start();39         }40 41         private void btn_Stop_Click(object sender, EventArgs e)42         {43             //停止计时44             this.timer.Stop();45         }46     }47 }复制代码2、System.Timers.Timer设计界面:后台代码:复制代码 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace TimersTimer11 {12     public partial class FrmMain : Form13     {14         //定义全局变量15         public int currentCount = 0;16         //定义Timer类17         System.Timers.Timer timer;18         //定义委托19         public delegate void SetControlValue(string value);20 21         public FrmMain()22         {23             InitializeComponent();24         }25 26         private void Form1_Load(object sender, EventArgs e)27         {28             InitTimer();29         }30 31         /// 32         /// 初始化Timer控件33         /// 34         private void InitTimer()35         {36             //设置定时间隔(毫秒为单位)37             int interval = 1000;38             timer = new System.Timers.Timer(interval);39             //设置执行一次(false)还是一直执行(true)40             timer.AutoReset = true;41             //设置是否执行System.Timers.Timer.Elapsed事件42             timer.Enabled = true;43             //绑定Elapsed事件44             timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);45         }46 47         /// 48         /// Timer类执行定时到点事件49         /// 50         /// 51         /// 52         private void TimerUp(object sender, System.Timers.ElapsedEventArgs e)53         {54             try55             {56                 currentCount += 1;57                 this.Invoke(new SetControlValue(SetTextBoxText),currentCount.ToString());58             }59             catch (Exception ex)60             {61                 MessageBox.Show("执行定时到点事件失败:" + ex.Message);62             }63         }64 65         /// 66         /// 设置文本框的值67         /// 68         /// 69         private void SetTextBoxText(string strValue)70         {71             this.txt_Count.Text = this.currentCount.ToString().Trim();72         }73 74         private void btn_Start_Click(object sender, EventArgs e)75         {76             timer.Start();77         }78 79         private void btn_Stop_Click(object sender, EventArgs e)80         {81             timer.Stop();82         }83     }84 }复制代码3、System.Threading.Timer设计界面:后台代码:复制代码 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Threading;10 11 namespace Threading.Timer12 {13     public partial class FrmMain : Form14     {15         //定义全局变量16         public int currentCount = 0;17         //定义Timer类18         System.Threading.Timer threadTimer;19         //定义委托20         public delegate void SetControlValue(object value);21 22         public FrmMain()23         {24             InitializeComponent();25         }26 27         private void FrmMain_Load(object sender, EventArgs e)28         {29             InitTimer();30         }31 32         /// 33         /// 初始化Timer类34         /// 35         private void InitTimer()36         {37             threadTimer = new System.Threading.Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, 1000);38         }39 40         /// 41         /// 定时到点执行的事件42         /// 43         /// 44         private void TimerUp(object value)45         {46             currentCount += 1;47             this.Invoke(new SetControlValue(SetTextBoxValue), currentCount);48         }49 50         /// 51         /// 给文本框赋值52         /// 53         /// 54         private void SetTextBoxValue(object value)55         {56             this.txt_Count.Text = value.ToString();57         }58 59         /// 60         /// 开始61         /// 62         /// 63         /// 64         private void btn_Start_Click(object sender, EventArgs e)65         {66             //立即开始计时,时间间隔1000毫秒67             threadTimer.Change(0, 1000);68         }69 70         /// 71         /// 停止72         /// 73         /// 74         /// 75         private void btn_Stop_Click(object sender, EventArgs e)76         {77             //停止计时78             threadTimer.Change(Timeout.Infinite, 1000);79         }80     }81 }

  

转载于:https://www.cnblogs.com/profession/p/8968312.html

你可能感兴趣的文章