简述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 ...
随机推荐
- Springboot 配置文件加解密
功能介绍 在Spring boot开发过程中,需要在配置文件里配置许多信息,如数据库的连接信息等,如果不加密,传明文,数据库就直接暴露了,相当于"裸奔"了,因此需要进行加密处理才行 ...
- 概率分布之间的距离度量以及python实现
1. 欧氏距离(Euclidean Distance) 欧氏距离是最易于理解的一种距离计算方法,源自欧氏空间中两点间的距离公式.(1)二维平面上两点a(x1,y1)与b(x2,y2)间的欧 ...
- WPF 实现指定UI控件截图
using System.Windows.Media.Imaging; using System.IO; private void SaveToImage(FrameworkElement ui, s ...
- leetcode232
public class MyQueue { Stack<int> S = new Stack<int>(); /** Initialize your data structu ...
- leetcode434
public class Solution { public int CountSegments(string s) { s = s.Trim(); ) { ; } else { ; ; ; i &l ...
- 迷你MVVM框架 avalonjs 0.83发布
本版本做了如下改进: 重构计算属性, 这是@soom提出的BUG,发现计算属性被某个监控属性向上驱动更新自己时,不会解发$watch回调.详见这里. 强化ms-bind绑定,当第一次扫描时也会执行它的 ...
- 在spring中使用quartz配置作业的二种方式
- Python运维开发基础09-函数基础
上节作业回顾 #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 实现简单的shell命令sed的替换功能 import ...
- mysql常用语句及关键字
一.常用sql语句 1.创建数据库userCREATE DATABASE user; 2.删除数据库userDROP DATABASE user; 3.使用数据库userUSE user;显示数据库 ...
- maven标签说明
<project xmlns="http://maven.apache.org/POM/4.0.0 " xmlns:xsi="http://www.w3.org/2 ...