简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别
System.Windows.Forms.Timer
基于窗体应用程序
阻塞同步
单线程
timer中处理时间较长则导致定时误差极大。
System.Timers.Timer
基于服务
非阻塞异步
多线程
/// <summary>
/// windows定时器
/// </summary>
System.Windows.Forms.Timer _wTimer;
/// <summary>
/// 应用程序生成定时器
/// </summary>
System.Timers.Timer _tTimer; private void Form1_Load(object sender, EventArgs e)
{
_wTimer = new System.Windows.Forms.Timer();
_wTimer.Interval = ;//设置时间间隔500毫秒
_wTimer.Tick += _wTimer_Tick;
_wTimer.Start();//启动定时器 _tTimer = new System.Timers.Timer();
_tTimer.Interval = ;//设置时间间隔500毫秒
_tTimer.Elapsed += _tTimer_Elapsed;
//_tTimer.Start(); }
void _tTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Print("_tTimer_Elapsed" + _count.ToString());
Thread.Sleep();//休眠2秒
_count++;
} void _wTimer_Tick(object sender, EventArgs e)
{
Print("_wTimer_Tick" + _count.ToString());
Thread.Sleep();//休眠2秒
_count++;
} private void Print(string msg)
{
if (this.InvokeRequired)
{
this.BeginInvoke((Action)delegate()
{
textBox1.AppendText(msg + "\r\n");
});
}
else
{
textBox1.AppendText(msg + "\r\n");
}
}
当启动_wTimer.Start(),输出结果。在_wTimer_Tick 休眠2秒阻塞主线程,导致程序假死2秒。而且事件没在没有处理完成不会进行下次,所以_count 结果每次只输出一次、。

当启动_tTimer.Start(),每隔500毫秒添加一个线程。由于异步只要到达时间间隔则自动生成下个线程,不管上个线程是否处理完成。
所以_count的结果会有多次输出。

如果想解决 system.timers 没到时间间隔只生成一个线程或者说上次没有处理完下次则等待处理可以在处理前添加_tTimer.stop(),完成后再次启动_tTimer.start()。
简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别的更多相关文章
- 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编辑器里用着还好好的,但是一导出就给我报错,让我十分不爽. 于是请教百度,搜出了五花八门的答案,没一个能解决我的问题的, ...
- System.Windows.Forms.Timer反编译学习
using System; using System.ComponentModel; using System.Globalization; using System.Runtime; using S ...
- .net chart(图表)控件的使用-System.Windows.Forms.DataVisualization.dll
这个案例指在介绍微软这套免费又功能强大的图表控件Microsoft Chart Controls for Microsoft .NET Framework 3.5,通过它,可让您的项目及报表,轻松套用 ...
- System.Windows.Forms
File: winforms\Managed\System\WinForms\DataGridView.cs Project: ndp\fx\src\System.Windows.Forms.cspr ...
- System.Windows.Forms.Control : Component, IOleControl, IOleObject, IOleInPlaceObject, IOleInPlaceActiveObject....
#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...
- System.Windows.Forms.ListView : Control
#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...
- System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)
.NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...
- 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 ...
随机推荐
- Basic64 编码解码
import sun.misc.BASE64Decoder; public class Base64 { /** * 字符串转Base64编码 * @param s * @return */ publ ...
- django多对多数据库建立 以及数据的传输 并进行增加 删除 修改
多对多数据库的建立 class Host(models.Model): nid = models.AutoField(primary_key=True) #自增id hostname = models ...
- javascript 对象的扩展性
javascript 对象 的可扩展性 javascript 对象中的可扩展性指的是:是否可以给对象添加新属性.所有的内置对象和自定义对象显示的都是可扩展的,对于宿主对象,则有javascript 引 ...
- AUC,KS,ROC
要弄明白ks值和auc值的关系首先要弄懂roc曲线和ks曲线是怎么画出来的.其实从某个角度上来讲ROC曲线和KS曲线是一回事,只是横纵坐标的取法不同而已.拿逻辑回归举例,模型训练完成之后每个样本都会得 ...
- SpringBoot进行单元测试
SpringBoot进行单元测试,需要在maven中加入以下依赖 <dependency> <groupId>org.springframework.boot</grou ...
- sqlserver流程控制(待续)
if else: if(1=1) begin--必须1个=号print '111'--begin end 之间必须要有内容end else beginprint '222'end while: DEC ...
- C和C++中的不定参数
在初学C的时候,我们都会用到printf函数来写Hello World的程序.在我们看printf函数的声明时,会看到类似于下面代码 int printf(const char * __restric ...
- 事务传播性、隔离性与MVCC
一.事务传播性 1.1 什么是事务的传播性 事务的传播性一般在事务嵌套时候使用,比如在事务A里面调用了另外一个使用事务的方法,那么这俩个事务是各自作为独立的事务执行提交,还是内层的事务合并到外层的事务 ...
- 编译驱动的Makefile解析
一个典型的编译驱动模块的Makefile文件如下所示: KERN_DIR = /root/driver/kernel obj-m += module_test.o all: make -C $(KER ...
- iphone在微信中audio 音频无法自动播放
问题: Html5的audio 音频在电脑端和android端都可以实现自动播放,在iphone上无法实现,下面针对的是微信浏览器里面的解决方法 html代码: <div id="au ...