.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. DELPHI2007 安装ACTIVEX插件的方法

    先新建一个Package    file----NEW-----Package Delphi for win32, 再在Component->Import Component里面添加好Activ ...

  2. C# 如何读取一行中的所有变量

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. Python - 求斐波那契数列前N项之和

    n = int(input("Input N: ")) a = 0 b = 1 sum = 0 for i in range(n): sum += a a, b = b, a + ...

  4. The Producer-Consumer Relationship

    //Listing 3-1. The Producer-Consumer Relationship Version 1 public class PC { public static void mai ...

  5. c#循环迭代匿名类链表(可迭代的匿名类)

    Main(){ //为什么?object是基类啊!! //报错.不能从List<anonymous>换成List<object>. //var q=(List<objec ...

  6. MongoDB上的索引

    1. 将索引建在number键上名为nameIndex并且为正序索引({number:-1}为倒序索引) 如: db.list名.ensureIndex({number:1},{name:" ...

  7. SQLSERVER 16进制与10进制转换

    最近工控项目中遇到的16进制与10进制转换,在.NET中比较容易实现,在SQLSERVER中发现没有直接的转换,尤其是出现超出范围的long负数,即无符号64位整数在sqlserver中的存储.网上找 ...

  8. 【转】Java魔法堂:String.format详解

    Java魔法堂:String.format详解     目录     一.前言    二.重载方法     三.占位符     四.对字符.字符串进行格式化     五.对整数进行格式化     六. ...

  9. Android实现app长时间未操作时自动退出app

    这里要考虑3个问题,第一个是锁屏问题,第二个是app被切换至后台的问题,第三个是屏幕锁定和解除时app在后台时的问题 一,监听屏幕解锁,锁定 ? 1 2 3 4 5 6 7 8 9 10 11 12 ...

  10. WPF--Blend制作Button控件模板--问题补充

    补充记录Button控件模板 控件模板制作过程中出现下图问题:动画对象不能用于动画属性"Fill” 并且这类问题Blend4中包括VS2010中仍然可以运行,但是只有VS2010中会报错:如 ...