C#中三种计时器使用异同点.docx

上传人:罗晋 文档编号:11695787 上传时间:2021-08-31 格式:DOCX 页数:9 大小:57.68KB
返回 下载 相关 举报
C#中三种计时器使用异同点.docx_第1页
第1页 / 共9页
C#中三种计时器使用异同点.docx_第2页
第2页 / 共9页
C#中三种计时器使用异同点.docx_第3页
第3页 / 共9页
C#中三种计时器使用异同点.docx_第4页
第4页 / 共9页
C#中三种计时器使用异同点.docx_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《C#中三种计时器使用异同点.docx》由会员分享,可在线阅读,更多相关《C#中三种计时器使用异同点.docx(9页珍藏版)》请在三一文库上搜索。

1、C#中三种计时器使用异同点c#强化系列文章三:实验分析C#中三种计时 器使用异同点C#中提供了三种类型的计时器:1、基于 Windows 的标准计时器(System.Windows.Forms.Timer)2、基于服务器的计时器(System.Timers.Timer)3、线程计时器(System.Threading.Timer)下面我就通过一些小实验来具体分析三种计时器使用上面的异同点,特别是和线程有关的部分。实验例子截图:一、基于 Windows 的标准计时器(System.Windows.Fonns.Timer)首先注意一点就是:Windows计时器是为单线程环境设计的此计时器从Visu

2、al Basic 1.0版起就存在于该产品中,并且基本上未做改动这个计时器是使用最简单的一种,只要把工具箱中的Timer控件拖到窗体上,然后设置一下事件和间隔时间等属性就可以了实验出来的结果也完全符合单线程的特点:1、当启动此计时器后,会在下方子线程ID列表中显示子线程ID,并且和主线程ID相同private void formsTimer_Tick(object sender, EventArgs e)I i+;IblSubThread.Text +=子线程执行,线程 ID: 11 + System.Threading.Thread.CurrentThread.ManagedThreadld

3、.ToStringO + rn;G System Windows Forms.Timer System.Timers. Timer,iDlxl线程静态变量值System. Threading. Timer启动 I 停止 I主税程暂停渺I主线程ID: 10子线程ID:一ZJUIDIDIDIDID1DIDID-J-J-QT- I 一 aT-rl QxoxaxozoxoTOTQTOT一 nd MJ a4J9c9c9c检名毋鱼2、当单击主线程暂停5秒后,子线程会暂停执行,并且当5秒之后不会执行之前被暂停的子 线程,而是直接执行后面的子线程(也就是会少输出几行值)System. Threading. T

4、hread.Sleep(5000);3、在子进程的事件中暂停5秒会导致主窗口相应无响应5秒4、定义一个线程静态变量:ThreadStaticprivate static int i = 0;在子线程事件中每次加一,再点击线程静态变量值会得到增加后的i值二、基于服务器的计时器(System.Timers.Timer)System.Timers.Timer不依赖窗体,是从线程池唤醒线程,是传统的计时器为了在服务器 环境上运行而优化后的更新版本在VS2005的工具箱中没有提供现成的控件,需要手工编码使用此计时器使用方式有两种,1、通过SynchronizingObject属性依附于窗体System.

5、Timers.Timer timersTimer = new System.Timers.Timer();timersTimer.Enabled = false;timersTimer.Interval = 100;timersTimer.Elapsed += new System.Timers.ElapsedEventHandl er(timersTimer_Elapsed);timersTimer.SynchronizingObject = this;通过这种方式来使用,实验效果几乎和基于Windows的标准计时器一样,只是在上面的第 二条实验中,虽然也会暂停子线程的执行,不过在5秒之后把

6、之前排队的任务都执行掉(也就是 不会少输出几行值)2、不使用 SynchronizingObject 属性这种方式就是多线程的方式了,即启动的子线程和主窗体不在一个线程。不过这样也存在一个 问题:由于子线程是单独的一个线程,那么就不能访问住窗体中的控件了,只能通过代理的方 式来访问:delegate void SetTextCa 11 back( stri ng text); void timersTimer_Elapsed(object sender, System.Timers.ElapsedEv entArgs e) BE I /使用代理I string text =子线程执行,线程 I

7、D: + System .Th read i ng .Th read. C urrentThread.ManagedThreadld.ToStringO + rrT;SetTextCallback d = new SetTextCa 11 back( SetText);申申 this.Invoke(d, new object text );I i+;private void SetText(stnng text)sIblSubThread.Text += text;这样我们再次实验就会得到如下的结果:1、当启动此计时器后,会在下方子线程ID列表中显示子线程ID,并且和主线程ID不相同圈线程测试

8、Jnl xlSystem. Timers.Timer System. Windows. Fwms. TimerSystem. Threading. T imer线程静态变量值启动停止主线程哲停5秒D D D D D D III I I I 程程程程程程 线线线线线线2、当单击主线程暂停5秒后,子线程会一直往下执行(界面上可能看不出来,不过通过在子线 程输出文件的方式可以很方便的看出来)3、在子进程的事件中行停5秒不会导致主窗口无响应4、在子线程事件中每次给线程静态变量加一,再点击线程静态变量值得到的值还是0(不会改 变主窗口中的线程静态变量)三、线程计时器(System.Threading.T

9、imer)线程计时也不依赖窗体,是一种简单的、轻量级计时,它使用回调方法而不是使用事件, 并由线程池线程提供支持。对消息不在线程上发送的方案中,线程计时器是非常有用的。使用方法如下:System .Th read i n g .Ti me r threadTimer;public void ThreadMethod(Object state)BE ZI/使用代理I string text =子线程执行,线程 ID: + System.Threading.Thread.C urrentThread.ManagedThreadld.ToStringO + rn;SetTextCallback d

10、= new SetTextCa 11 back( SetText);中年 this.Invoke(d, new object text );I i+;L private void Forml_Load(object sender, EventArgs e)E threadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(ThreadMethod)z null, -lz -1);暂停代码:threadTimer.Change(-lf -1);实验的效果和基于服务器的计时器(System. Timers. T

11、imer)的第二种方式是一样的, 当然具体的使用方法和原理是不一样的,最主要的就是这种方式使用的是代理的方式而不是事 件的方式,并且可以不依赖于窗体和组件而单独执行F面列出老外总结的一张浅(三种方式的区别):FeaturedescriptionSystem.Timers.TimerSystem.Th readi ng.TimerSystem. Windows. Form s.TimerSupport for adding and removing listeners after theYesNoYestimer is instantiated.Supports c all backs on t

12、he user-interf ace threadYesNoYesCalls back from threads obtained from the thread poolYesYesNoSupports drag-and-d rop in the Windows Forms DesignerYesNoYesSuitable for running in a server multi-threa ded environme ntYesYesNoIncludessupport for passingNoYesNoarbitrary state from the timer initializatio ii to the callback.Implement sIDisposableYesYesYesSupports one-off callbacks as well as periodic repeating callbacksYesYesYesAccessible across application domain boundariesYesYesYesSupportsIComponent一 hostablein an工Con七ainerYesNoYes

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1