.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)的更多相关文章

  1. 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编辑器里用着还好好的,但是一导出就给我报错,让我十分不爽. 于是请教百度,搜出了五花八门的答案,没一个能解决我的问题的, ...

  2. System.Windows.Forms.Timer反编译学习

    using System; using System.ComponentModel; using System.Globalization; using System.Runtime; using S ...

  3. .net chart(图表)控件的使用-System.Windows.Forms.DataVisualization.dll

    这个案例指在介绍微软这套免费又功能强大的图表控件Microsoft Chart Controls for Microsoft .NET Framework 3.5,通过它,可让您的项目及报表,轻松套用 ...

  4. System.Windows.Forms

    File: winforms\Managed\System\WinForms\DataGridView.cs Project: ndp\fx\src\System.Windows.Forms.cspr ...

  5. System.Windows.Forms.Control : Component, IOleControl, IOleObject, IOleInPlaceObject, IOleInPlaceActiveObject....

    #region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...

  6. System.Windows.Forms.ListView : Control

    #region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...

  7. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法

    System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...

  8. 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别

    System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...

  9. 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 ...

随机推荐

  1. jquery 选择器,模糊匹配

    按姓名匹配 1,name前缀为aa的所有div的jquery对象 $("div[name^='aa']"); 2,name后缀为aa的所有div的jquery对象 $(" ...

  2. NERDTree 快捷键辑录

    参看连接:http://www.cnblogs.com/lexus/archive/2012/11/04/2753187.html http://yang3wei.github.io/blog/201 ...

  3. [dpdk] 读开发指南(2)(内容长期整理中)

    接续前节. 7 PMD (Poll Mode Driver) A Poll Mode Driver (PMD) consists of APIs, provided through the BSD d ...

  4. 数据库留言板例题:session和cookie区别

    session和cookie区别: <?php session_start(); //session_start();必须写在所有的php代码前边 ?> <!DOCTYPE html ...

  5. MFC函数之BitBlt

    MFC函数之BitBlt // Cgame123View 绘制 void Cgame123View::OnDraw(CDC* pDC) { Cgame123Doc* pDoc = GetDocumen ...

  6. NRF51822之动态广播使用

    本教程基于nRF51_SDK_10.0.0_dc26b5e\examples\ble_peripheral\ble_app_uart工程 本教程主要是演示 现在演示通过nus来修改ADV中maufac ...

  7. Qt设置系统时间(使用SetSystemTime API函数)

    大家都知道Qt中有QDateTime等有关时间与日期的类,类中包含很多成员函数,可以很方便的实现有关时间与日期的操作,比如:想要获得系统当前的时间与日期,可以调用currentDateTime();  ...

  8. SQL SERVER中非聚集索引的覆盖,连接,交叉,过滤

    1.覆盖索引:select和where中包含的结果集中应存在“非聚集索引列”,这样就不用查找基表了,索引表即可搞定:   2.索引交叉:索引的交叉可以理解成建立多个非聚集索引之间的join,如表实体一 ...

  9. android Listview item 中有button,item就不响应触摸事件

    为button设置 beanButton.getButton().setFocusable(false); beanButton.getButton().setFocusableInTouchMode ...

  10. 网页上的表格数据table

    格式: <table> <tr> <th> </th> </tr> <tr> <td> </td> &l ...