C#中5中timer的比较
C#中有5个timer,它们的主要区别如下:
- System.Threading.Timer 在线程池启动一个后台任务。我前段时间写过一个关于timer的垃圾回收的需要注意一下,参见谁动了我的timer?。
- System.Windows.Forms.Timer 告诉windows把一个计时器和调用它的线程(UI线程)关联起来,通过往UI线程的消息队列里放一个WM_TIMER的消息来实现,所以它的callback一定是在UI线程调用的,不存在多线程调用的问题。
- System.Windows.Threading.DispatcherTimer 用在WPF和Silverlight中,对应于System.Windows.Forms.Timer。
- Windows.UI.Xaml.Dispatchertimer 用在windows store app中,对应于System.Windows.Forms.Timer。
- 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的比较的更多相关文章
- [C#].NET中几种Timer的使用
这篇博客将梳理一下.NET中4个Timer类,及其用法. 1. System.Threading.Timer public Timer(TimerCallback callback, object s ...
- Windows Service中使用Threading.Timer需注意回收
在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...
- Firebug中调试中的js脚本中中文内容显示为乱码
Firebug中调试中的js脚本中中文内容显示为乱码 设置 页面 UFT-8 编码没用, 解决方法:点击 "Firebug"工具栏 中的"选项"---" ...
- C#中DataTable中的Compute方法使用收集
原文: C#中DataTable中的Compute方法使用收集 Compute函数的参数就两个:Expression,和Filter. Expresstion是计算表达式,关于Expression的详 ...
- js中== 和===中的区别
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- win7中VS2010中安装CSS3.0问题解决方法
win7中VS2010中安装CSS3.0问题解决方法 在安装Standards Update for VS2010 SP1后,VS2010中没有CSS3.0问题,以下是我的解决方法 1.首先去官网 ...
- cocos2dx常见的46中+22中动作详解
cocos2dx常见的46中+22中动作详解 分类: iOS2013-10-16 00:44 1429人阅读 评论(0) 收藏 举报 bool HelloWorld::init(){ ///// ...
- Nhibernate中 Many-To-One 中lazy="proxy" 延迟不起作用的原因
2010-07-15 12:10 by 彭白洋, 322 阅读, 0 评论, 收藏, 编辑 NHibernate中 Many-To-One 中lazy="proxy" 延迟不起作用 ...
- IOS获取物理尺寸中7Plus中获取的是7的物理尺寸
IOS获取物理尺寸中7Plus中获取的是7的物理尺寸: 在开发调试过程中我的7Plus手机获取[uiscreen mainscreen].bounds为750 .1334. 解决方案:在手机中的显示 ...
- 【转】Java中字符串中子串的查找共有四种方法(indexof())
原文网址:http://wfly2004.blog.163.com/blog/static/1176427201032692927349/ Java中字符串中子串的查找共有四种方法,如下:1.int ...
随机推荐
- Android 中LinearLayout控件属性
id 为控件指定相应的ID text 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符 g ...
- 基于jq图片居中插件 [center]
最近在做一个项目,大量的图片基于js进行缩放(图片放大镜),考虑用css要写许多hack,而已经基于jq了,干脆写个方法得了. 代码很简单,不用多讲但是很实用. $.fn.extend({ cente ...
- Ajax跨域访问问题-方法大全
Case I. Web代理的方式 (on Server A) 即用户访问A网站时所产生的对B网站的跨域访问请求均提交到A网站的指定页面,由该页面代替用户页面完成交互,从而返回合适的结果.此方案可以解决 ...
- Centos7 修改运行级别
systemd使用比sysvinit的运行级别更为自由的target概念作为替代 第三运行级: multi-user.target 第五运行级: graphical.target #前者是符号链接 ...
- 如何在eclips下将一段代码抽取为方法Extract Method
最近读了读关于重构的文章,做了个小总结(在编程思想目录下<从文章"避免复制与粘贴"到文章"Extract Method"的反思 系列>). 然后因为 ...
- Xcode-程序开发设计-01UIKit 框架
CGRect中的前缀:CoreGraphics.frameworksUIView中的前缀:User InterFace 属于UIKit的框架NS前缀,NS是NextStep 对象方法:辞去第1响应者- ...
- IntelIoT技术笔记Maven
1.Maven project facet配置 错误信息: One or more constraints have not been satisfied.以及Cannot change versio ...
- virtalBox共享文件夹设置
sudo mount -t vboxsf gongxiang /mnt/shared/
- node场景
http://www.zhihu.com/question/19653241 http://www.csdn.net/article/2012-05-03/2805296 http://limu.it ...
- 【uva10917】Walk Through the Forest (最短路)
题目: gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共 ...