System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)
.NET Framework里面提供了三种Timer:
- System.Windows.Forms.Timer
- System.Timers.Timer
- System.Threading.Timer
VS.Net 2005默认只有一个Timer控件,但那是System.Forms.Timer控件.如果要使用System.Timers.Timer的控件,需要在工具箱上单击右键,手动添加.
添加的步骤:工具箱单击右键->Add Item->找到命名空间是System.Timers.Timer的控件,将其选中,OK即可。
这里简单的介绍一下这两种Timer的区别。
System.Windows.Forms.Timer是使用得比较多的Timer,Timer Start之后定时(按设定的Interval)调用挂接在Tick事件上的EvnetHandler。在这种Timer的EventHandler中可 以直接获取和修改UI元素而不会出现问题--因为这种Timer实际上就是在UI线程自身上进行调用的。也正是因为这个原因,导致了在Timer的 EventHandler里面进行长时间的阻塞调用,将会阻塞界面响应的后果。下面是一个简单的例子:
public class MainForm : Form
{ private void MainForm_Load(object sender, EventArgs e)
{
timer.Interval = ;
timer.Tick += delegate(object o, EventArgs args)
{
DoWork();
};
timer.Start();
} private void DoWork()
{
for (int i = ; i < ; i++)
{
System.Threading.Thread.Sleep();
}
}
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
}
在这个例子中,DoWork方法里面将会阻塞10秒,在这10秒之内,UI将会失去响应。而通过使用System.Timers.Timer,就可 以解决这个问题。因为System.Timers.Timer是在.NET的Thread Pool上面运行的,而不是直接在UI Thread上面运行,所以在这种Timer的EventHandler里面进行耗时较长的计算不会导致UI失去响应。但是这里有两个地方需要注意:
因为一般来说System.Timers.Timer不是运行在UI Thread上面的,所以如果要在这种Timer的EventHandler里面更新UI元素的话,需要进行一次线程切换,在WinForm开发中一般通过UI元素的Invoke方法完成:
private void DoWork()
{
for (int i = ; i < ; i++)
{
System.Threading.Thread.Sleep();
}
this.Invoke(new UpdateUICallBack(UpdateUI));
} private delegate void UpdateUICallBack(); private void UpdateUI()
{
}
System.Timers.Timer有一个Property:SynchronizingObject 。如果设置了这个Property(一般是某个Form),那么之后对Timer挂接的EventHandler的调用将会在创建这个UI元素的线程上进 行(一般来说就是UI线程)。值得注意的是,如果你通过WinForm设计器把System.Timers.Timer拖放到Form上,那么这个 Property将会自动被设置。此时这种Timer就和System.Windows.Forms.Timer的效果一样:长调用将会阻塞界面。
System.Windows.Forms.Timer
1. 它是一个基于Form的计时器
2. 创建之后,你可以使用Interval设置Tick之间的跨度,用委托(delegate)hook Tick事件
3. 调用Start和Stop方法,开始和停止
4. 完全基于UI线程,因此部分UI相关的操作会在这个计时器内进行
5. 长时间的UI操作可能导致部分Tick丢失
System.Timers.Timer
1. 用的不是Tick事件,而是Elapsed事件
2. 和System.Windows.Forms.Timer一样,用Start和Stop方法
3. AutoReset属性决定计时器是不是要发起一次事件然后停止,还是进入开始/等待的循环。System.Windows.Forms.Timer没有这个属性
4. 设置对于UI控件的同步对象(synchronizing object),对控件的UI线程发起事件
这两者与System.Threading.Timer的区别比较见http://mark.michaelis.net/Blog/SystemWindowsFormsTimerVsSystemThreadingTimerVsSystemTimersTimer.aspx,这里只摘录最后的总结:
System.Windows.Forms.Timer 是对于用户界面编程的比较显然的选择。而另外两个之间的选择就不是很明显。如果必须在IContainer内,那么就应该选择 System.Timers.Timer。如果没有用到System.Timers.Timer的特性,那么建议选择 System.Threading.Timer,因为它稍稍轻量级一些。
Timer 是先等待再执行,如果我们要达到先执行再等待的效果,设置 默认时间间隔Interval =100,或者更少为1(不能为0),之后再引发事件内更改 时间间隔Interval 为想要的值。
System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)的更多相关文章
- ArgumentException: The Assembly Mono.WebBrowser is referenced by System.Windows.Forms ('Assets/Plugins/System.Windows.Forms.dll'). But the dll is not allowed to be included or could not be found.
最近有个项目要用到System.Windows.Forms.dll,在Unity编辑器里用着还好好的,但是一导出就给我报错,让我十分不爽. 于是请教百度,搜出了五花八门的答案,没一个能解决我的问题的, ...
- System.Windows.Forms.Timer反编译学习
using System; using System.ComponentModel; using System.Globalization; using System.Runtime; using S ...
- .net chart(图表)控件的使用-System.Windows.Forms.DataVisualization.dll
这个案例指在介绍微软这套免费又功能强大的图表控件Microsoft Chart Controls for Microsoft .NET Framework 3.5,通过它,可让您的项目及报表,轻松套用 ...
- System.Windows.Forms
File: winforms\Managed\System\WinForms\DataGridView.cs Project: ndp\fx\src\System.Windows.Forms.cspr ...
- System.Windows.Forms.Control : Component, IOleControl, IOleObject, IOleInPlaceObject, IOleInPlaceActiveObject....
#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...
- System.Windows.Forms.ListView : Control
#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...
- 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别
System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用
System.Windows.Forms.Timer.System.Timers.Timer.System.Threading.Timer的 区别和用法http://space.itpub.net/1 ...
随机推荐
- 20145317《Java程序设计》实验三
20145317彭垚 实验三<敏捷开发与XP实践> 实验内容 使用 git 上传代码 使用 git 相互更改代码 实现代码的重载 一. 使用git 上传代码 找到Demo1所在文件夹,右键 ...
- java--构造器初始化
(1)不含自变量的构造器 构建是在基础类的“外部”进行的,所以基础类会在衍生类访问它之前得到正确的初始化. (2)含有自变量的构造器 在衍生类构建器中,首先对基础类构建器的调用,也就是在调用构造器之前 ...
- TCPdump抓包命令详解--摘
http://blog.csdn.net/s_k_yliu/article/details/6665673/ http://starsliao.blog.163.com/blog/static/890 ...
- concurrency parallel 并发 并行
Computer Systems A Programmer's Perspective Second Edition The general phenomenon of multiple flows ...
- IMAP和POP3有什么区别
http://help.163.com/10/0203/13/5UJONJ4I00753VB8.html?servCode=6010237 IMAP和POP3有什么区别? POP3协议允许电子邮件 ...
- Android系统用于Activity的标准Intent
1 根据联系人ID显示联系人信息 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); //显示联系人信息 int ...
- 在 mac os 上搭建 git server
前言:之前学习了如何使用 git 后,一直想搭建一个本机搭建一个 git server 的,一开始不知道走了弯路用了 gitosis,折腾了我好几天都没配置好.昨晚查资料发现 gitosis 早就过时 ...
- library not found for -lPods 的解决办法
在老项目工程中使用cocoapods,可能会报这个错误:library not found for -lPods . 导致这个错误可能有两个原因,这两个原因在编译过程中都是有蛛丝马迹可循的. 原因1: ...
- C++ 实现 发送HTTP Get/Post请求 good
1.简述 最近简单看了一下关于HTTP请求方面的知识,之前一直用Qt来实现,有专门HTTP请求的QNetworkAccessManager类来处理,实现也比较简单,这里主要讲解一下用C++代码来实现H ...
- node.js的exprots工厂模式
工厂类: /** * Created by zzq on 2015/5/15. */ module.exports = function(){ this.getProduct = function() ...