您的位置:首页 > 编程语言 > C#

实验分析C#中三种计时器使用异同点

2008-12-23 11:03 357 查看
C#中提供了三种类型的计时器:
1、基于 Windows 的标准计时器(System.Windows.Forms.Timer)
2、基于服务器的计时器(System.Timers.Timer)
3、线程计时器(System.Threading.Timer)
下面我就通过一些小实验来具体分析三种计时器使用上面的异同点,特别是和线程有关的部分。
实验例子截图:



一、基于 Windows 的标准计时器(System.Windows.Forms.Timer)
首先注意一点就是:Windows 计时器是为单线程环境设计的
此计时器从Visual Basic 1.0 版起就存在于该产品中,并且基本上未做改动
这个计时器是使用最简单的一种,只要把工具箱中的Timer控件拖到窗体上,然后设置一下事件和间隔时间等属性就可以了

实验出来的结果也完全符合单线程的特点:
1、当启动此计时器后,会在下方子线程ID列表中显示子线程ID,并且和主线程ID相同


private void formsTimer_Tick(object sender, EventArgs e)






{


i++;


lblSubThread.Text += "子线程执行,线程ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + "/r/n";


}





2、当单击主线程暂停5秒后,子线程会暂停执行,并且当5秒之后不会执行之前被暂停的子线程,而是直接执行后面的子线程(也就是会少输出几行值)
System.Threading.Thread.Sleep(5000);
3、在子进程的事件中暂停5秒会导致主窗口相应无响应5秒
4、定义一个线程静态变量:
[ThreadStatic]
private static int i = 0;
在子线程事件中每次加一,再点击线程静态变量值会得到增加后的i值

二、基于服务器的计时器(System.Timers.Timer)
System.Timers.Timer不依赖窗体,是从线程池唤醒线程,是传统的计时器为了在服务器环境上运行而优化后的更新版本
在VS2005的工具箱中没有提供现成的控件,需要手工编码使用此计时器
使用方式有两种,
1、通过SynchronizingObject属性依附于窗体


System.Timers.Timer timersTimer = new System.Timers.Timer();




timersTimer.Enabled = false;


timersTimer.Interval = 100;


timersTimer.Elapsed += new System.Timers.ElapsedEventHandler(timersTimer_Elapsed);




timersTimer.SynchronizingObject = this;



通过这种方式来使用,实验效果几乎和基于 Windows 的标准计时器一样,只是在上面的第二条实验中,虽然也会暂停子线程的执行,不过在5秒之后把之前排队的任务都执行掉(也就是不会少输出几行值)
2、不使用SynchronizingObject属性
这种方式就是多线程的方式了,即启动的子线程和主窗体不在一个线程。不过这样也存在一个问题:由于子线程是单独的一个线程,那么就不能访问住窗体中的控件了,只能通过代理的方式来访问:


delegate void SetTextCallback(string text);






.






.


void timersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)






{


//使用代理


string text = "子线程执行,线程ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + "/r/n";


SetTextCallback d = new SetTextCallback(SetText);




this.Invoke(d, new object[]

{ text });


i++;


}




private void SetText(string text)






{


lblSubThread.Text += text;


}
这样我们再次实验就会得到如下的结果:
1、当启动此计时器后,会在下方子线程ID列表中显示子线程ID,并且和主线程ID不相同



2、当单击主线程暂停5秒后,子线程会一直往下执行(界面上可能看不出来,不过通过在子线程输出文件的方式可以很方便的看出来)
3、在子进程的事件中暂停5秒不会导致主窗口无响应
4、在子线程事件中每次给线程静态变量加一,再点击线程静态变量值得到的值还是0(不会改变主窗口中的线程静态变量)

三、线程计时器(System.Threading.Timer)
线程计时器也不依赖窗体,是一种简单的、轻量级计时器,它使用回调方法而不是使用事件,并由线程池线程提供支持。
对消息不在线程上发送的方案中,线程计时器是非常有用的。
使用方法如下:


System.Threading.Timer threadTimer;


public void ThreadMethod(Object state)






{


//使用代理


string text = "子线程执行,线程ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + "/r/n";


SetTextCallback d = new SetTextCallback(SetText);




this.Invoke(d, new object[]

{ text });


i++;


}




private void Form1_Load(object sender, EventArgs e)






{


threadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(ThreadMethod), null, -1, -1);


}





暂停代码:
threadTimer.Change(-1, -1);

实验的效果和基于服务器的计时器(System.Timers.Timer)的第二种方式是一样的,
当然具体的使用方法和原理是不一样的,最主要的就是这种方式使用的是代理的方式而不是事件的方式,并且可以不依赖于窗体和组件而单独执行

下面列出老外总结的一张表(三种方式的区别):

Feature descriptionSystem.Timers.TimerSystem.Threading.TimerSystem.Windows.Forms.Timer
Support for adding and removing listeners after the timer is instantiated.YesNoYes
Supports call backs on the user-interface threadYesNoYes
Calls back from threads obtained from the thread poolYesYesNo
Supports drag-and-drop in the Windows Forms DesignerYesNoYes
Suitable for running in a server multi-threaded environmentYesYesNo
Includes support for passing arbitrary state from the timer initialization to the callback.NoYesNo
Implements IDisposableYesYesYes
Supports one-off callbacks as well as periodic repeating callbacksYesYesYes
Accessible across application domain boundariesYesYesYes
Supports IComponent – hostable in an IContainerYesNoYes
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: