System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer

System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,
但是另外两个没有这个情况,System.Timers.Timer、System.Threading.Timer!
System.Timers.Timer、System.Threading.Timer这两个平时用的时候没有发现太大的区别,定时的精度都差不多。
一般我个人用的话,还是用的System.Threading.Timer比较多,用编程的方法比较好用!
System.Windows.Forms.Timer是基于UI的
System.Timers.Timer是基于服务
System.Threading.Timer是基于线程

除了Timer只能用于界面,其他的事在没看出区别
System.Windows.Forms.Timer 还有 WPF那个timer是单线程的 单次运行时间过长会影响下次触发 精度差
System.Threading.Timer   都是多线程的 单次运行时间过长不会影响下次触发

精度高
System.Threading.Timer 是一个简单的轻量计时器,它使用回调方法并由线程池线程提供服务。不建议将其用于 Windows 窗体,因为其回调不在用户界面线程上进行。System.Windows.Forms.Timer 是用于 Windows 窗体的更佳选择。要获取基于服务器的计时器功能,可以考虑使用 System.Timers.Timer,它可以引发事件并具有其他功能。

在《CLR Via C#》中讲多线程时有提到这3个计时器,但作者说System.Timers.Timer是对System.Threading.Timer的报装,不推荐使用,但是在我的WEB项目中的Application_Start中我还是使用的这个而不是Threading.Timer,因为使用Threading.Timer时只执行了一次就不在执行了。

对于计时器在B/S结构中的使用就复杂一些,一般我们把计时器放在Application_OnStart中,这样全局维护一个计时器,可以进行定期备份数据库,定期维护用户等操作,而且方法写作静态的,以免被垃圾回收。而不建议在一般的aspx页面中使用,因为服务器端的定时器对用户这样意义不大,完全可以使用JS代替。而且这个页面的每个请求都可能引入一个新的定时器,导致系统崩溃。另外,定时器是ASP.NET进程,IIS有关,所以对用重要的执行任务,还是建议写成服务或独立程序放在服务器上执行好了。
参考:http://blog.itpub.net/12639172/viewspace-571197

出处:http://www.cnblogs.com/tianciliangen/p/4910503.html

原文地址:https://www.cnblogs.com/WangPingjing/p/11219423.html

时间: 2024-08-04 03:26:25

System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的相关文章

【Winform】 无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”。

问题:将dotnet framework 4.0 切换到2.0时,编译没有问题,在运行时出现如下错误:System.InvalidCastException: 无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”. 解决方法: 注释掉如下代码(此代码为设计器自动生成代码) //((System.ComponentModel.ISupportInitialize

无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”

解决办法: 在设计器代码中把4.0自动加上去的代码注释掉就行了. //((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();  //((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit(); 按不同的版本进行条件编译 #if V2007#else ((System.ComponentModel.IS

System.Windows.Forms.Control : Component, IOleControl, IOleObject, IOleInPlaceObject, IOleInPlaceActiveObject....

#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll #endregion using System.Collections; using System.ComponentModel; using Syst

System.Windows.Forms.ListView : Control

#region 程序集 System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll #endregion using System.Collections; using System.ComponentModel; using Syst

System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用

一.System.Windows.Forms.Timer 1.基于Windows消息循环,用事件方式触发,在界面线程执行:是使用得比较多的Timer,Timer Start之后定时(按设定的Interval)调用挂接在Tick事件上的EvnetHandler.在这种Timer的EventHandler中可 以直接获取和修改UI元素而不会出现问题--因为这种Timer实际上就是在UI线程自身上进行调用的. 2.它是一个基于Form的计时器3.创建之后,你可以使用Interval设置Tick之间的跨

System.Windows.Forms.Timer反编译学习

1 using System; 2 using System.ComponentModel; 3 using System.Globalization; 4 using System.Runtime; 5 using System.Runtime.InteropServices; 6 using System.Threading; 7 namespace System.Windows.Forms 8 { 9 [DefaultEvent("Tick"), DefaultProperty(

System.Windows.Forms

1 File: winforms\Managed\System\WinForms\DataGridView.cs 2 Project: ndp\fx\src\System.Windows.Forms.csproj (System.Windows.Forms) 3 4 //------------------------------------------------------------------------------ 5 // <copyright file="DataGridVi

WPF 添加ACTIVEX 控件 ——System.Windows.Forms.AxHost+InvalidActiveXStateException异常

---恢复内容开始--- 一.ACTIVEX及其相关概念 COM(Component Object Model,对象组件模型): DCOM(Distributed COM,分布式对象组件模型): CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构): 动态数据交换(Dynamic Data Exchange,DDE),解决了数据的更新,但数据格式仍是问题: 对象的链接与嵌入(Object Linking and Embedde

【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别

原文:[C#遗补]获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别 .Net Framework中,System.IO.Directory.GetCurrentDirectory()方法用于获得应用程序当前工作目录 如果使用此方法获得应用程序所在的目录,应该注意:System.IO.Directory.GetCurrentDirectory()方法获得的