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