zoukankan      html  css  js  c++  java
  • C#三种定时器的实现

    ·关于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() + " ";

      }

      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() + " ";

      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() + " ";

      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 description 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

  • 相关阅读:
    URAL——DFS找规律——Nudnik Photographer
    URAL1353——DP——Milliard Vasya's Function
    URAL1203——DPor贪心——Scientific Conference
    递推DP HDOJ 5389 Zero Escape
    区间DP UVA 1351 String Compression
    树形DP UVA 1292 Strategic game
    Manacher HDOJ 5371 Hotaru's problem
    同余模定理 HDOJ 5373 The shortest problem
    递推DP HDOJ 5375 Gray code
    最大子序列和 HDOJ 1003 Max Sum
  • 原文地址:https://www.cnblogs.com/zhoug2020/p/5837035.html
Copyright © 2011-2022 走看看