架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 10863|回复: 1

[资料] C#三种定时器的实现

[复制链接]
发表于 2017-10-12 13:12:51 | 显示全部楼层 |阅读模式
关于C#中timer类 在C#里关于定时器类就有3个

1.定义在System.Windows.Forms里

2.定义在System.Threading.Timer类里

3.定义在System.Timers.Timer类里



System.Windows.Forms.Timer是应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中 的Timer控件,内部使用API SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console Application(控制台应用程序)无法使用。



System.Timers.Timer和System.Threading.Timer非常类似,它们是通过.NET Thread Pool实现的,轻量,计时精确,对应用程序、消息没有特别的要求。System.Timers.Timer还可以应用于WinForm,完全取代上面的 Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。



例:

使用System.Timers.Timer类

System.Timers.Timer t = new System.Timers.Timer(10000);//实例化Timer类,设置间隔时间为10000毫秒;

t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;

t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);

t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;



public void theout(object source, System.Timers.ElapsedEventArgs e)

{

MessageBox.Show("OK!");

}



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

http://dotnet.chinaitlab.com/CSharp/737740.html



  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);



来源:(http://blog.sina.com.cn/s/blog_5aeeb8200100bhc4.html) - (转)C#中三种定时器对象的比较_dash_新浪博客

  。



  。



  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 descrip{过滤}tion System.Timers.Timer System.Threading.Timer System.Windows.Forms.Timer



  Support for adding and removing listeners after the timer is instantiated. Yes No Yes



  Supports call backs on the user-interface thread Yes No Yes



  Calls back from threads obtained from the thread pool Yes Yes No



  Supports drag-and-drop in the Windows Forms Designer Yes No Yes



  Suitable for running in a server multi-threaded environment Yes Yes No



  Includes support for passing arbitrary state from the timer initialization to the callback. No Yes No



  Implements IDisposable Yes Yes Yes



  Supports one-off callbacks as well as periodic repeating callbacks Yes Yes Yes



  Accessible across application domain boundaries Yes Yes Yes



  Supports IComponent – hostable in an IContainer Yes No Yes




上一篇:c# Lamda表达式集合去重分组取最小值
下一篇:Linq:切勿使用 Count() > 0 来判断集合非空
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-11-8 14:02:44 | 显示全部楼层
真的。确实不错但是为什么没有回帖
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-6-16 16:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表