[C#].NET中幾種Timer的使用實例
這篇博客將梳理一下.NET中4個Timer類,及其用法。
1. System.Threading.Timer
public Timer(TimerCallback callback, object state, int dueTime, int period);
callback委托將會在period時間間隔內(nèi)重復(fù)執(zhí)行,state參數(shù)可以傳入想在callback委托中處理的對象,dueTime標(biāo)識多久后callback開始執(zhí)行,period標(biāo)識多久執(zhí)行一次callback。
using System.Threading;
// System.Threading.Timer
Timer timer = new Timer(delegate
{
 Console.WriteLine($"Timer Thread: {Thread.CurrentThread.ManagedThreadId}");
 Console.WriteLine($"Is Thread Pool: {Thread.CurrentThread.IsThreadPoolThread}");
 Console.WriteLine("Timer Action.");
},
null,
2000,
1000
);
Console.WriteLine("Main Action.");
Console.WriteLine($"Main Thread: {Thread.CurrentThread.ManagedThreadId}");
Console.ReadLine();
Timer回掉方法執(zhí)行是在另外ThreadPool中一條新線程中執(zhí)行的。
2. System.Timers.Timer
System.Timers.Timer和System.Threading.Timer相比,提供了更多的屬性,
Interval 指定執(zhí)行Elapsed事件的時間間隔;
Elapsed 指定定期執(zhí)行的事件;
Enabled 用于Start/Stop Timer;
Start 開啟Timer
Stop 停止Timer
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 500;
timer.Elapsed += delegate
{
 Console.WriteLine($"Timer Thread: {Thread.CurrentThread.ManagedThreadId}");
 Console.WriteLine($"Is Thread Pool: {Thread.CurrentThread.IsThreadPoolThread}");
 Console.WriteLine("Timer Action");
 timer.Stop();
};
timer.Start();
Console.WriteLine("Main Action.");
Console.WriteLine($"Main Thread: {Thread.CurrentThread.ManagedThreadId}");
Console.ReadLine();
Timer Elapsed定期任務(wù)是在ThreadPool的線程中執(zhí)行的。
3. System.Windows.Forms.Timer
Interval 指定執(zhí)行Elapsed事件的時間間隔;
Tick 指定定期執(zhí)行的事件;
Enabled 用于Start/Stop Timer;
Start 開啟Timer
Stop 停止Timer
使用System.Windows.Forms.Timer來更新窗體中Label內(nèi)時間,
using System.Windows.Forms;
public Form1()
{
 InitializeComponent();
 this.Load += delegate
 {
  Timer timer = new Timer();
  timer.Interval = 500;
  timer.Tick += delegate
  {
   System.Diagnostics.Debug.WriteLine($"Timer Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
   System.Diagnostics.Debug.WriteLine($"Is Thread Pool: {System.Threading.Thread.CurrentThread.IsThreadPoolThread}");
   this.lblTimer.Text = DateTime.Now.ToLongTimeString();
  };
  timer.Start();
  System.Diagnostics.Debug.WriteLine($"Main Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
 };
}
Timer Tick事件中執(zhí)行的事件線程與主窗體的線程是同一個,并沒有創(chuàng)建新線程(或者使用ThreadPool中線程)來更新UI。下面將代碼做一個改動,使用System.Timers.Timer來更新UI上的時間,代碼如下,
public Form1()
{
 InitializeComponent();
 this.Load += delegate
 {
  System.Timers.Timer timer = new System.Timers.Timer();
  timer.Interval = 500;
  timer.Elapsed += delegate
  {
   System.Diagnostics.Debug.WriteLine($"Timer Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
   System.Diagnostics.Debug.WriteLine($"Is Thread Pool: {System.Threading.Thread.CurrentThread.IsThreadPoolThread}");
   this.lblTimer.Text = DateTime.Now.ToLongTimeString();
  };
  timer.Start();
  System.Diagnostics.Debug.WriteLine($"Main Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
 };
}
很熟悉的一個錯誤。因為Label是由UI線程創(chuàng)建的,所以對其進行修改需要在UI線程中進行。System.Timers.Timer中Elasped執(zhí)行是在ThreadPool中新創(chuàng)建的線程中執(zhí)行的。所以會有上面的錯誤。
4. System.Windows.Threading.DispatcherTimer
屬性和方法與System.Windows.Forms.Timer類似。
using System.Windows.Threading;
public MainWindow()
{
 InitializeComponent();
 this.Loaded += delegate
 {
  //DispatcherTimer
  DispatcherTimer timer = new DispatcherTimer();
  timer.Interval = TimeSpan.FromSeconds(1);
  timer.Start();
  Debug.WriteLine($"Main Thread Id: {Thread.CurrentThread.ManagedThreadId}");
  timer.Tick += delegate
  {
   tbTime.Text = DateTime.Now.ToLongTimeString();
   Debug.WriteLine($"Timer Thread Id: {Thread.CurrentThread.ManagedThreadId}");
   timer.Stop();
  };
 };
}
DispatcherTimer中Tick事件執(zhí)行是在主線程中進行的。
使用DispatcherTimer時有一點需要注意,因為DispatcherTimer的Tick事件是排在Dispatcher隊列中的,當(dāng)系統(tǒng)在高負荷時,不能保證在Interval時間段執(zhí)行,可能會有輕微的延遲,但是絕對可以保證Tick的執(zhí)行不會早于Interval設(shè)置的時間。如果對Tick執(zhí)行時間準確性高可以設(shè)置DispatcherTimer的priority。例如:
DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Send);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#中利用LINQ to XML與反射把任意類型的泛型集合轉(zhuǎn)換成XML格式字符串的方法
欄 目:C#教程
下一篇:C#批量插入數(shù)據(jù)到Sqlserver中的三種方式
本文標(biāo)題:[C#].NET中幾種Timer的使用實例
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6095.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
 - 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
 - 01-10C#實現(xiàn)由四周向中心縮小的窗體退出特效
 - 01-10Extjs4如何處理后臺json數(shù)據(jù)中日期和時間
 - 01-10.net2.0+ Winform項目實現(xiàn)彈出容器層
 - 01-10C#中DataGridView常用操作實例小結(jié)
 - 01-10C#編程獲取資源文件中圖片的方法
 - 01-10asp.net中XML如何做增刪改查操作
 - 01-10C#利用反射技術(shù)實現(xiàn)去掉按鈕選中時的邊框效果
 - 01-10C#中查找Dictionary中的重復(fù)值的方法
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實現(xiàn)txt定位指定行完整實例
 - 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
 - 01-10C#停止線程的方法
 - 01-10C#實現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機閱讀
- 04-02jquery與jsp,用jquery
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10delphi制作wav文件的方法
 - 01-10C#中split用法實例總結(jié)
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-11ajax實現(xiàn)頁面的局部加載
 


