C#三種定時器的實現

原文地址:https://www.cnblogs.com/wwwbdabc/p/11280667.htmlhtml

·關於C#中timer類 在C#裏關於定時器類就有3個服務器

1.定義在System.Windows.Forms裏多線程

2.定義在System.Threading.Timer類裏app

3.定義在System.Timers.Timer類裏dom

 

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控件。它們的缺點是不支持直接的拖放,須要手工編碼。post

 

例:優化

使用System.Timers.Timer類ui

System.Timers.Timer t = new System.Timers.Timer(10000);//實例化Timer類,設置間隔時間爲10000毫秒;this

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#中提供了三種類型的計時器:

 

  一、基於 Windows 的標準計時器(System.Windows.Forms.Timer)

 

  二、基於服務器的計時器(System.Timers.Timer)

 

  三、線程計時器(System.Threading.Timer)

 

  下面我就經過一些小實驗來具體分析三種計時器使用上面的異同點,特別是和線程有關的部分。

 

  實驗例子截圖:

 

 

 

  1、基於 Windows 的標準計時器(System.Windows.Forms.Timer)

 

  首先注意一點就是:Windows 計時器是爲單線程環境設計的

 

  此計時器從Visual Basic 1.0 版起就存在於該產品中,而且基本上未作改動

 

  這個計時器是使用最簡單的一種,只要把工具箱中的Timer控件拖到窗體上,而後設置一下事件和間隔時間等屬性就能夠了

 

  實驗出來的結果也徹底符合單線程的特色:

 

  一、當啓動此計時器後,會在下方子線程ID列表中顯示子線程ID,而且和主線程ID相同

 

  private void formsTimer_Tick(object sender, EventArgs e)

 

  {

 

  i++;

 

  lblSubThread.Text += "子線程執行,線程ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + "\r\n";

 

  }

 

 

 

 

  二、當單擊主線程暫停5秒後,子線程會暫停執行,而且當5秒以後不會執行以前被暫停的子線程,而是直接執行後面的子線程(也就是會少輸出幾行值)

 

  System.Threading.Thread.Sleep(5000);

 

  三、在子進程的事件中暫停5秒會致使主窗口相應無響應5秒

 

  四、定義一個線程靜態變量:

 

  [ThreadStatic]

 

  private static int i = 0;

 

  在子線程事件中每次加一,再點擊線程靜態變量值會獲得增長後的i值

 

  2、基於服務器的計時器(System.Timers.Timer)

 

  System.Timers.Timer不依賴窗體,是從線程池喚醒線程,是傳統的計時器爲了在服務器環境上運行而優化後的更新版本

 

  在VS2005的工具箱中沒有提供現成的控件,須要手工編碼使用此計時器

 

  使用方式有兩種,

 

  一、經過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秒以後把以前排隊的任務都執行掉(也就是不會少輸出幾行值)

 

  二、不使用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;

 

  }

 

  這樣咱們再次實驗就會獲得以下的結果:

 

  一、當啓動此計時器後,會在下方子線程ID列表中顯示子線程ID,而且和主線程ID不相同

 

 

 

  二、當單擊主線程暫停5秒後,子線程會一直往下執行(界面上可能看不出來,不過經過在子線程輸出文件的方式能夠很方便的看出來)

 

  三、在子進程的事件中暫停5秒不會致使主窗口無響應

 

  四、在子線程事件中每次給線程靜態變量加一,再點擊線程靜態變量值獲得的值仍是0(不會改變主窗口中的線程靜態變量)

 

  3、線程計時器(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 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

相關文章
相關標籤/搜索