@开发者,一文搞懂什么是 C# 计时器!|CSDN 博文精选
作者 | 朱钢
责编 | 郭芮
Timer 计时器是在 C# 开发中经常用到的,但是有很多开发人员对它并不了解,今天这篇文就具体讲解一下 C# 中的计时器。
在 C# 中存在3种常用的 Timer :
-
System.Windows.Forms.Timer
-
System.Timers.Timer
-
System.Threading.Timer
System.Windows.Forms.Timer
这个 Timer 是单线程的,也就是说只要它运行,其他线程就要等着。
这个 Timer 有如下特点:
-
完全基于 UI 线程,定时器触发时,操作系统把定时器消息插入线程消息队列中,调用线程执行一个消息泵提取消息,然后发送到回调方法 Tick 中;
-
使用 Start 和 Stop 启动和停止 Timer;
-
UI 操作过长会导致 Tick 丢失;
-
可以使用委托 Hook Tick 事件;
-
精确度不高;
-
通过将 Enabled 设置为 True,使 Timer 自动运行。
从上面的第一个特点可得知,该 Timer 会造成 WinForm UI 假死,因此如果需要定时处理大量计算或者大量 IO 操作的任务,不建议使用该 Timer。接下来我们看一个例子体会一下在IO操作的情况下出现的假死情况。
我们在 Form 中放入两个 Button、一个 Lable 和一个 Timer:
[code]private void Button_Click(object sender, EventArgs e){ timer.Interval = 1000; timer.Tick += Timer_Tick; timer.Start();}private void Timer_Tick(object sender, EventArgs e){ for (int i = 0; i < 10000; i++) { File.AppendAllText(Directory.GetCurrentDirectory()+"test.txt", i.ToString()); this.label_output.Text = "当前操作:插入数字" + i; }} timer.Interval = 1000; timer.Tick += Timer_Tick; timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { for (int i = 0; i < 10000; i++) { File.AppendAllText(Directory.GetCurrentDirectory()+"test.txt", i.ToString()); this.label_output.Text = "当前操作:插入数字" + i; } }
我们单击计算按钮,我们会发现 WinForm 出现了假死(无法移动窗口、按钮无法点击等)。
System.Timers.Timer
该 Timer 基于服务器,是为在多线程环境中用于辅助线程而设计的,可以在线程间移动来处理引发的 Elapsed 事件,比上一个计时器更加精确。
该 Timer 有如下特点:
-
通过 Elapsed 设置回掉处理事件,且 Elapsed 是运行在 ThreadPool 上的;
-
通过 Interval 设置间隔时间;
-
当 AutoReset 设置为 False 时,只在到达第一次时间间隔后触发 Elapsed 事件;
-
是一个多线程计时器;
-
无法直接调用 WinForm 上的控件,需要使用委托;
-
主要用在 Windows 服务中。
同样我们通过代码来看一下该 Timer 计时器怎么使用:
[code]System.Timers.Timer timersTimer = new System.Timers.Timer();private void Button_Click(object sender, EventArgs e){ timersTimer.Interval = 1000; timersTimer.Enabled = true; timersTimer.Elapsed += TimersTimer_Elapsed; timersTimer.Start();}private void TimersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e){ for (int i = 0; i < 10000; i++) { this.BeginInvoke(new Action(() => { this.label_output.Text="当前时间:"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }), null); }}private void Button1_Click(object sender, EventArgs e){ timersTimer.Stop();} private void Button_Click(object sender, EventArgs e) { timersTimer.Interval = 1000; timersTimer.Enabled = true; timersTimer.Elapsed += TimersTimer_Elapsed; timersTimer.Start(); } private void TimersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { for (int i = 0; i < 10000; i++) { this.BeginInvoke(new Action(() => { this.label_output.Text="当前时间:"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }), null); } } private void Button1_Click(object sender, EventArgs e) { timersTimer.Stop(); }
运行上面代码,会发现 WinForm 界面假死的情况消失了。
System.Threading.Timer
该 Timer 同样也是一个多线程的计时器,它有如下特点:
-
多线程;
-
和前两个计时器相比没有 Start 和 Stop 方法,如果要停止计时器,必须调用 Dispose 方法来销毁 Timer 对象;
-
调用 Dispose 方法后并不能马上停止所有的计时器,这是因为间隔时间小于执行时间时多个线程运行造成的,多个线程无法同时停止;
-
是一个轻量级的计时器;
-
所有的参数全部在构造函数中进行了设置;
-
可以设置启动时间;
-
不建议在 WinForm 程序中使用。
我们来看一下代码(在控制台应用程序中输入以下代码):
[code]static System.Threading.Timer threadingTimer;static int numSum = 0;static void Main(string[] args){ threadingTimer = new System.Threading.Timer(new System.Threading.TimerCallback(threadingTimer_Elapsed), null, 0, 1000); Console.Read();}private static void threadingTimer_Elapsed(object state){ for (int i = 0; i < 10000; i++) { numSum++; Console.WriteLine("输出数字:"+i); } if (numSum > 10000) { threadingTimer.Dispose(); Console.WriteLine("结束"); }} static int numSum = 0; static void Main(string[] args) { threadingTimer = new System.Threading.Timer(new System.Threading.TimerCallback(threadingTimer_Elapsed), null, 0, 1000); Console.Read(); } private static void threadingTimer_Elapsed(object state) { for (int i = 0; i < 10000; i++) { numSum++; Console.WriteLine("输出数字:"+i); } if (numSum > 10000) { threadingTimer.Dispose(); Console.WriteLine("结束"); } }
注意,当我们不再需要多线程 Timer 计时器的时候,我们可以调用 Dispose 方法释放所占有的资源。但是因为 Timer 计时器是按线程池线程来安排回调执行的,因此回调可能发生在 Dispose 方法的重载被调用之后,所以我们可以使用可使用 Dispose(WaitHandle) 方法等待所有回掉完成。
总结
综上所属我们总结出 C# 中不同 Timer 计时器的特点和使用环境:
作者:朱钢,.NET高级开发工程师,7年一线开发经验,参与过电子政务系统和AI客服系统的开发,以及互联网招聘网站的架构设计,目前就职于北京恒创融慧科技发展有限公司。
本文系作者投稿,版权归其所有,首发于CSDN博客,原文链接:
https://blog.csdn.net/gangzhucoll/article/details/93744022
2019程序员转型学什么?
[strong] 热 文 推 荐 [/strong]
☞将 30 万行代码从 Flow 迁移到 TypeScript 是一种怎样的体验?
☞中国有微信和支付宝, 你为啥还费力不讨好去做区块链? | 人物志
你点的每个“在看”,我都认真当成了喜欢
- @开发者,一文搞懂什么是 C# 计时器!|CSDN 博文精选
- 转载CSDN博文精选:Android系列开发博客资源汇总
- CSDN博文精选:最受欢迎的系列专栏博客推荐
- CSDN博文精选:最受欢迎的系列专栏博客推荐
- CSDN博文精选:最受欢迎的系列专栏博客推荐
- CSDN博文精选:最受欢迎的系列专栏博客推荐
- 「词嵌入」在自然语言处理中扮演什么角色?一文搞懂Word Embeddings的背后原理
- C#正则表达式之组与非捕获组浅析 2009-08-20 13:44 chenglidexiaoxue CSDN博客 字号:T | T 一键收藏,随时查看,分享好友! C#正则表达式之组与非捕获组是什么
- CSDN博文精选:iPhone系列开发博客资源汇总
- CSDN博文精选:Android系列开发博客资源汇总
- CSDN博文精选:Android系列开发博客资源汇总
- CSDN博文精选 微信小程序全方位解析
- CSDN博文精选:最受欢迎的系列专栏博客推荐
- CSDN博文精选:Android系列开发博客资源汇总
- CSDN博文精选:Android系列开发博客资源汇总
- 如何利用 Android 自定义控件实现炫酷的动画?|CSDN 博文精选
- CSDN博文精选:最受欢迎的系列专栏博客推荐
- CSDN博文精选:iPhone系列开发博客资源汇总
- 转载CSDN博文精选:Android系列开发博客资源汇总
- GitHub 排行榜 C 位出道:手把手教你玩转 V 语言版的俄罗斯方块!|CSDN 博文精选...