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. opencv:级联分类器训练(cascade classifier training)(两个分类器的区别)

    # 介绍 级联分类器包括两个工作阶段:训练(traning),检测(detection).检测阶段在文档<objdetect module of general OpenCV documenta ...

  2. sql日期查询

    select getdate() ,getdate()) ,getdate()) ) ,getdate())) ,getdate())) Select datename(weekday, getdat ...

  3. java过滤关键词

    敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有必要的.前段时间我一个朋友(马上毕业,接触编程不久)要我帮他看一个文字过滤的东西,它说检索效率非常慢.我把它程序拿过来 ...

  4. linux shell脚本编程笔记(四): 获取字符串长度的七种方法

    获取字符串长度的七种方法 1. \${#str} 2.awk的length 备注:1) 最好用{}来放置变量2) 也可以用length($0)来统计文件中每行的长度 3.awk的NF 备注: -F为分 ...

  5. jQuery的选择器的总结

    一.简单选择器 // $(function () { // $("#box").css("color","red") // }) // 这个 ...

  6. basicHttpBinding

    表示一个绑定,Windows Communication Foundation (WCF) 服务可以使用此绑定配置和公开能够与基于 ASMX 的 Web 服务和客户端通信的终结点以及符合 WS-I B ...

  7. 获取iPod library中的媒体文件

    [获取iPod library中的媒体文件] The Media Player framework provides facilities for playing movie, music, audi ...

  8. ViewController的属性

    [ViewController的属性] 1.navigationItem,只读,只第一次引用的时候被创建. The first time the property is accessed, the U ...

  9. git 回退到某个特定提交

    1.先用git log commit aba290c570d3894f4f39a1fdf52aa512c0231525 Author: huzhengbo <@qq.com> Date: ...

  10. python多版本共存问题

    1.环境变量配置,pip路径别忘记加入,否则pip不好使. 2.如果改名python.exe为其他名字,复制一份保留,否则pip容易无法启动进程 参见爆栈: http://stackoverflow. ...