解析.NET中幾種Timer的運用。本站提示廣大學習愛好者:(解析.NET中幾種Timer的運用)文章只能為提供參考,不一定能成為您想要的結果。以下是解析.NET中幾種Timer的運用正文
這篇博客將梳理一下.NET中4個Timer類,及其用法。
1. System.Threading.Timer
public Timer(TimerCallback callback, object state, int dueTime, int period);
callback委托將會在period時間距離內反復執行,state參數可以傳入想在callback委托中處置的對象,dueTime標識多久後callback開端執行,period標識多久執行一次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,
);
Console.WriteLine("Main Action.");
Console.WriteLine($"Main Thread: {Thread.CurrentThread.ManagedThreadId}");
Console.ReadLine();

Timer回掉辦法執行是在另外ThreadPool中一條新線程中執行的。
2. System.Timers.Timer
System.Timers.Timer和System.Threading.Timer相比,提供了更多的屬性,
Interval 指定執行Elapsed事情的時間距離;
Elapsed 指定活期執行的事情;
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活期義務是在ThreadPool的線程中執行的。
3. System.Windows.Forms.Timer
Interval 指定執行Elapsed事情的時間距離;
Tick 指定活期執行的事情;
Enabled 用於Start/Stop Timer;
Start 開啟Timer
Stop 中止Timer
運用System.Windows.Forms.Timer來更新窗體中Label內時間,
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事情中執行的事情線程與主窗體的線程是同一個,並沒有創立新線程(或許運用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線程創立的,所以對其停止修正需求在UI線程中停止。System.Timers.Timer中Elasped執行是在ThreadPool中新創立的線程中執行的。所以會有下面的錯誤。
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事情執行是在主線程中停止的。
運用DispatcherTimer時有一點需求留意,由於DispatcherTimer的Tick事情是排在Dispatcher隊列中的,當零碎在高負荷時,不能保證在Interval時間段執行,能夠會有細微的延遲,但是相對可以保證Tick的執行不會早於Interval設置的時間。假如對Tick執行時間精確性高可以設置DispatcherTimer的priority。例如:
DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Send);
以上就是本文的全部內容,希望本文的內容對大家的學習或許任務能帶來一定的協助,同時也希望多多支持!