.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. 20145235《Java程序设计》第7周学习总结

    教材学习内容总结 13.1 认识时间与日期 格林威治时间(GMT):通过观察太阳而得,因为地球公转轨道为椭圆形且速度不一,本身自传减速而造成误差. 世界时(UT):通过观测远方星体跨过子午线而得,受地 ...

  2. HTML: 字符實體

    在HTML編寫中,有些字符不適合直接在代碼中寫出,比如>,<, (space空格),",',&等等,這時我們按照一定的格式將它們寫出,比如(大於)> 符號,我們用 ...

  3. EXT.NET入门必读

    Ext.Net是一个对ExtJS进行封装了的.net控件库,可以在ASP.NET WebForm和MVC中使用.从今天开始记录我的学习笔记,这是第一篇,今天学习了如何在WebForm中使用Ext.Ne ...

  4. 灰度图像 Grayscale Binary_image

    https://en.wikipedia.org/wiki/Grayscale https://zh.wikipedia.org/wiki/灰度图像 In photography and comput ...

  5. SQL优化(zhuan)

    转自:http://www.jfox.info/SQL-you-hua 数据库的优化问题 一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出S ...

  6. json格式的转换为json字符串函数

    function toJSON(object){ var type = typeof object; if ('object' == type) { if (Array == object.const ...

  7. JBoss的安装与配置(对应eclipse配置)【转】

    安装JBoss纯粹是目的就是学习EJB3...至少现在是这样的 EJB需要运行在EJB容器中.每个J2EE应用服务器都含有EJB容器和Web容器.这样,既支持运行EJB,也可以运行Web应用 目前EJ ...

  8. 低功耗蓝牙4.0BLE编程-nrf51822开发(11)-蓝牙串口代码分析

    代码实例:点击打开链接 实现的功能是从uart口发送数据至另一个蓝牙串口,或是从蓝牙读取数据通过uart打印出数据. int main(void) { // Initialize leds_init( ...

  9. Solr4.3之检索建议suggest

    原文链接:http://www.656463.com/article/Efm26v.htm 很多才学solr的人,都容易把solr spellcheck和solr suggest混淆,误以为他们是一样 ...

  10. 转: css box-sizing的用法

    當你設定一個元素樣式為 box-sizing: border-box;,這個元素的內距和邊框將不會增加元素本身的寬度. <!DOCTYPE html> <html lang=&quo ...