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. 服务器选型:x86 vs 小型机谁更胜一筹?

    市场上关于X86 和小型机的争论从来就没有停止过,在以往的印象当中,x86服务器在中低端形成了统治之势,而小型机则在关键性应用领域(金融.证券.政府等)享有王者地位.但是随着X86服务器的不断发展,这 ...

  2. Hadoop 2.7.3 安装配置及测试

    1.概述 Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.hadoop三种安装模式:单机模式,伪分布式,真正分布式.因在实际生 ...

  3. 修改eclipse 代码字体大小以及文档字体大小

    1..点击[window]在弹出的窗口中选择[preferences] 2.在弹出窗口中找到依次点击General(常规)——Apprearance(外观)——Colors and Fonts(颜色和 ...

  4. Rhythmk 学习 Hibernate 09 - Hibernate HQL

    1.初始数据 @Test public void test01() { Session session = null; try { session = HibernateUtil.getSession ...

  5. php使用curl库进行ssl双向认证

    官方文档: http://www.php.net/manual/zh/function.curl-setopt.php#10692 官方举例: <?phpcurl_setopt($ch, CUR ...

  6. k8s gpu 资源设置

    将所有相同型号显卡的node打上 相同的label kubectl label node ogs-gpu02 gpu_type=k20m 启动device plugin 和app 时: nodeSel ...

  7. CLR如何控制类型中的字段排序

    [StructLayout(LayoutKind.Sequential) ]保持字段布局 [StructLayout(LayoutKind.Auto) ]CLR自动选择最优 [StructLayout ...

  8. js练习 原型

    //var a = {        //    fun: function a() {        //        test = 0;        //        alert(this) ...

  9. python之private variable

    [python之private variable] Since there is a valid use-case for class-private members (namely to avoid ...

  10. python性能测试脚本-乾颐堂

    废话不多说,直接上代码. import httplib import urllib import time import json     class Transaction(object):     ...