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用法区别的更多相关文章

  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的区别(zz)

    .NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...

  8. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法

    System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...

  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. 深度解析Java中的那把锁

    锁的本质 我们先来讨论锁的出现是为了解决什么问题,锁要保证的事情其实很好理解,同一件事(一个代码块)在同一时刻只能由一个人(线程)操作. 这里所说的锁为排他锁,暂不考虑读写锁的情况 我们在这里打个比方 ...

  2. 小米笔记本pro CPU GPU 做科学计算的算力对比

    小米笔记本pro:15.6寸,i7-8850,16G,256G,GPU:MX150 测试对象Caffe,MNIST训练 使用纯CPU训练: 1.耗时:11分58秒 2.功耗:35W 使用GPU训练: ...

  3. spring quartz 配置及说明

    方式一,jobDetail的bean利用MethodInvokingJobDetailFactoryBean 工厂包装 : ()定义一个bean,执行具体的业务操作. <bean id=&quo ...

  4. C++Builder 内存泄露检测

    C++Builder 内存泄露检测 CodeGuard http://bbs.2cto.com/read.php?tid=179933 XE新版里 ReportMemoryLeaksOnShutdow ...

  5. iscroll源码学习(1)

    iscroll是移端端开发的两大利器之一(另一个是fastclick),为了将它整合的avalon,需要对它认真学习一番.下面是我的笔记. 第一天看的是它的工具类util.js //用于做函数节流 v ...

  6. springmvc框架自带的异常处理器SimpleMappingExceptionResolver的使用

    使用分三步 1.定义异常类 2.在处理器中的用法 3.在springmvc配置文件中需要加配置

  7. nodejs-supervisor

    小技巧——使用 supervisor 如果你有 PHP 开发经验,会习惯在修改 PHP 脚本后直接刷新浏览器以观察结果,而你在开发 Node.js 实现的 HTTP 应用时会发现,无论你修改了代码的哪 ...

  8. 什么是First-class citizen?

    [什么是First-class citizen?] In programming language design, a first-class citizen (also type, object,  ...

  9. VUE递归树形目录(vue递归组件)的使用

    1.html <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  10. 基于注解的方式管理Bean

    --------------------siwuxie095                                 基于注解的方式管理 Bean         (一)准备         ...