.NET Framework里面提供了三种Timer:
- System.Windows.Forms.Timer
- System.Timers.Timer
- System.Threading.Timer
Visual Studio 2003的工具箱里面默认提供了System.Windows.Forms.Timer和System.Timers.Timer两种,而Visual Studio 2005中确只默认提供了System.Windows.Forms.Timer这一种。这里简单的介绍一下这两种Timer的区别。
System.Windows.Forms.Timer是使用得比较多的Timer,Timer Start之后定时(按设定的Interval)调用挂接在Tick事件上的EvnetHandler。在这种Timer的EventHandler中可以直接获取和修改UI元素而不会出现问题--因为这种Timer实际上就是在UI线程自身上进行调用的。也正是因为这个原因,导致了在Timer的EventHandler里面进行长时间的阻塞调用,将会阻塞界面响应的后果。下面是一个简单的例子:
public class MainForm : Form
{
private void MainForm_Load(object sender, EventArgs e)
{
timer.Interval = 1000;
timer.Tick += delegate(object o, EventArgs args)
{
DoWork();
};
timer.Start();
}
private void DoWork()
{
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(1000);
}
}
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
}
在这个例子中,DoWork方法里面将会阻塞10秒,在这10秒之内,UI将会失去响应。而通过使用System.Timers.Timer,就可以解决这个问题。因为System.Timers.Timer是在.NET的Thread Pool上面运行的,而不是直接在UI Thread上面运行,所以在这种Timer的EventHandler里面进行耗时较长的计算不会导致UI失去响应。但是这里有两个地方需要注意:
- 因为一般来说System.Timers.Timer不是运行在UI Thread上面的,所以如果要在这种Timer的EventHandler里面更新UI元素的话,需要进行一次线程切换,在WinForm开发中一般通过UI元素的Invoke方法完成:
private void DoWork()
{
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(1000);
}
this.Invoke(new UpdateUICallBack(UpdateUI));
}
private delegate void UpdateUICallBack();
private void UpdateUI()
{
}
- System.Timers.Timer有一个Property:SynchronizingObject 。如果设置了这个Property(一般是某个Form),那么之后对Timer挂接的EventHandler的调用将会在创建这个UI元素的线程上进行(一般来说就是UI线程)。值得注意的是,如果你通过WinForm设计器把System.Timers.Timer拖放到Form上,那么这个Property将会自动被设置。此时这种Timer就和System.Windows.Forms.Timer的效果一样:长调用将会阻塞界面。
分享到:
相关推荐
① System.Windows.Forms.Timer ② System.Timers.Timer ③ System.Threading.Timer 现分述如下: 一、System.Windows.Forms.Timer 1、基于Windows消息循环,用事件方式触发,在界面线程执行;是使用得比较多的Timer...
c# 精确定时器,精确Time 比System.Threading.Timer、System.Windows.Forms.Timer、System.Timers.Timer精准
1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Timers.Timer类里
C#定时器实现例子,包括三种实现方式System.Windows.Forms、System.Timers、System.Threading.Timer
System.Threading.Timer 是一个简单的轻量计时器... 多线程计时器1:System.Threading.Timer2:System.Timers.Timer特殊目的的单线程计时器:1:System.Windows.Forms.Timer(Windows Forms Timer)2:System.Windows.
C#定时器的三种实现方法(System.Timers.Timer、System.Windows.Forms.Timer、System.Threading.Timer)
1 System.Windows.Forms.Timer(使用环境:Windows Forms Timer) 2 System.Windows.Threading.DispatcherTimer( 使用环境:WPF timer); 单线程计时器比较安全, 因为运行在主线程中,对于更新 Windows Forms或者WPF 中...
关于C# Timer类 在C#里关于...(1)System.Windows.Forms.Timer 应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API SetTimer实现的。它的主要缺点是计时不精确,而
定义在System.Timers.Timer类里 System.Windows.Forms.Timer是应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API SetTimer实现的。它的主要缺点是计时不精确,而且...
(1)定义在System.Windows.Forms里 (2)定义在System.Threading.Timer类里 (3)定义在System.Timers.Timer类里 Timer 用于以用户定义的事件间隔触发事件。Windows 计时器是为单线程环境设计的,其中,UI 线程...
.NET Framework中为我们提供了3种类型的Timer,分别是: Server Timer(System.Timers.Timer),Thread Timer(System.Threading.Timer )和Windows Timer(System.Windows.Forms.Timer)。 其中Windows Timer和...
1、基于 Windows 的标准计时器(System.Windows.Forms.Timer) 2、基于服务器的计时器(System.Timers.Timer) 3、线程计时器(System.Threading.Timer) 一、基于 Windows 的标准计时器(System.Windows.Forms.Timer) 首先...
大家在开发过程中,经常遇到需要定时的功能...1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Timers.Timer类里 本定时器经过多个项目实际验证,精度达到1ms,自动校准,非常好用
1.2 Programs areTranslated byOtherPrograms intoDifferent Forms . . . . . . . . . . . . . . . 3 1.3 ItPays toUnderstandHowCompilation SystemsWork . . . . . . . . . . . . . . . . . . . . 4 1.4 ...
C# 中有三种定时器,System.Windows.Forms 中的定时器和 System.Timers.Timer 的工作方式是完全一样的,所以,这里我们仅讨论 System.Timers.Timer 和 System.Threading.Timer 1、定时器保活 先来看一个例子: ...
using System.Windows.Forms; using System.Data.SqlClient; using System.Timers; //Download by http://www.codefans.net namespace qwe { public partial class F_main : Form { //定义SqlDataAdapter的引用 ...
A cool simulation for anyone who might think about playing the stocks and spending money and get a general idea how the system works. <END><br>83,LoanExpert.zip A bank loan type project that lets ...