C#中有5个timer,它们的主要区别如下:

  1. System.Threading.Timer  在线程池启动一个后台任务。我前段时间写过一个关于timer的垃圾回收的需要注意一下,参见谁动了我的timer?
  2. System.Windows.Forms.Timer  告诉windows把一个计时器和调用它的线程(UI线程)关联起来,通过往UI线程的消息队列里放一个WM_TIMER的消息来实现,所以它的callback一定是在UI线程调用的,不存在多线程调用的问题。
  3. System.Windows.Threading.DispatcherTimer 用在WPF和Silverlight中,对应于System.Windows.Forms.Timer。
  4. Windows.UI.Xaml.Dispatchertimer 用在windows store app中,对应于System.Windows.Forms.Timer。
  5. System.Timers.Timer 包装了一下System.Threading.Timer,使它有了和System.Windows.Forms.Timer类似的接口,而且也能在visual studio的toolbox designer里找到。它也是在线程池中执行,但是如果你是在visual studio的designer中使用它,visual studio会自动把它所在的control设为这个timer的SynchronizingObject,这样就会保证callback会在UI线程调用了。Jeffrey Richter不建议使用它,建议直接用System.Threading.Timer。这个timer也有个坑,参见.NET 2.0的Timer elapsed event 会自动catch住所有的exception

Comparing the Timer Classes in the .NET Framework Class Library也比较了3中timer(System.Threading.Timer ,System.Windows.Forms.Timer和System.Timers.Timer),并且画了个如下的表格。

  System.Windows.Forms System.Timers System.Threading
Timer event runs on what thread? UI thread UI or worker thread Worker thread
Instances are thread safe? No Yes No
Familiar/intuitive object model? Yes Yes No
Requires Windows Forms? Yes No No
Metronome-quality beat? No Yes* Yes*
Timer event supports state object? No No Yes
Initial timer event can be scheduled? No No Yes
Class supports inheritance? Yes Yes No
* Depending on the availability of system resources (for example, worker threads)
 

C#中5中timer的比较的更多相关文章

  1. [C#].NET中几种Timer的使用

    这篇博客将梳理一下.NET中4个Timer类,及其用法. 1. System.Threading.Timer public Timer(TimerCallback callback, object s ...

  2. Windows Service中使用Threading.Timer需注意回收

    在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...

  3. Firebug中调试中的js脚本中中文内容显示为乱码

    Firebug中调试中的js脚本中中文内容显示为乱码 设置 页面 UFT-8 编码没用, 解决方法:点击 "Firebug"工具栏 中的"选项"---" ...

  4. C#中DataTable中的Compute方法使用收集

    原文: C#中DataTable中的Compute方法使用收集 Compute函数的参数就两个:Expression,和Filter. Expresstion是计算表达式,关于Expression的详 ...

  5. js中== 和===中的区别

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. win7中VS2010中安装CSS3.0问题解决方法

    win7中VS2010中安装CSS3.0问题解决方法   在安装Standards Update for VS2010 SP1后,VS2010中没有CSS3.0问题,以下是我的解决方法 1.首先去官网 ...

  7. cocos2dx常见的46中+22中动作详解

    cocos2dx常见的46中+22中动作详解 分类: iOS2013-10-16 00:44 1429人阅读 评论(0) 收藏 举报 bool HelloWorld::init(){    ///// ...

  8. Nhibernate中 Many-To-One 中lazy="proxy" 延迟不起作用的原因

    2010-07-15 12:10 by 彭白洋, 322 阅读, 0 评论, 收藏, 编辑 NHibernate中 Many-To-One 中lazy="proxy" 延迟不起作用 ...

  9. IOS获取物理尺寸中7Plus中获取的是7的物理尺寸

    IOS获取物理尺寸中7Plus中获取的是7的物理尺寸: 在开发调试过程中我的7Plus手机获取[uiscreen mainscreen].bounds为750  .1334. 解决方案:在手机中的显示 ...

  10. 【转】Java中字符串中子串的查找共有四种方法(indexof())

    原文网址:http://wfly2004.blog.163.com/blog/static/1176427201032692927349/ Java中字符串中子串的查找共有四种方法,如下:1.int ...

随机推荐

  1. Linux运维需要掌握的技能 (转)

    本人是linux运维工程师,对这方面有点心得,现在我说说要掌握哪方面的工具吧说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具.我就大概列出这几方面,这样入门就基本没问题了 ...

  2. 2016021801 - Java内存区域学习笔记

    根据<深入理解java虚拟机>学习归纳整理学习笔记 程序计数器 用途:当前线程的字节码文件的行号指示器.(当前机场负责控制飞机降落的空管员:当前线程表示当前机场, 所执行的字节码等同于被等 ...

  3. void (*f(int, void (*)(int)))(int) 函数解析 转

    今天与几个同学看到了一个函数指针定义: void (*f(int, void (*)(int)))(int) 以前在C trap pit fails里面见过,但是文章里面介绍的很详细,但是往往使初学者 ...

  4. Sharepoint 问题集锦 - external list (外部列表)

    使用Sharepoint开发过程中遇到的问题总结. 错误1: Unable to display this Web Part. To troubleshoot the problem, open th ...

  5. Prototype:Copy和Clone

    原型模式在C#中的实现比较直接,因为只需要继承了IClone的接口,就可以通过重写Clone方法,调用MemberwiseClone()来实现ProtoType的方式. class Test:IClo ...

  6. iOS 实现脉冲雷达以及动态增减元素 By Swift-感谢分享

    Swift经过Xcode6 Beta4一版更新后,基本上已经可以作为生产工具了,虽然有一些地方和ObjC比起来要“落后”一些,但也无伤大雅.这里就用Xcode6 Beta4+iOS SDK 8.0开发 ...

  7. webStorm中的混乱代码格式化

    Mac上    command + alt + l windows上 control + alt + l

  8. tyvj P1716 - 上帝造题的七分钟 二维树状数组区间查询及修改 二维线段树

    P1716 - 上帝造题的七分钟 From Riatre    Normal (OI)总时限:50s    内存限制:128MB    代码长度限制:64KB 背景 Background 裸体就意味着 ...

  9. L1、L2范式及稀疏性约束

    L1.L2范式及稀疏性约束 假设需要求解的目标函数为: E(x) = f(x) + r(x) 其中f(x)为损失函数,用来评价模型训练损失,必须是任意的可微凸函数,r(x)为规范化约束因子,用来对模型 ...

  10. MySQL中group_concat函数,用符号连接查询分组里字段值

    http://blog.csdn.net/my_yang/article/details/7186844