這篇文章主要介紹了C#四種計時器Timer怎么使用的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C#四種計時器Timer怎么使用文章都會有所收獲,下面我們一起來看看吧。
1、最底層、輕量級的計時器?;诰€程池實現的,工作在輔助線程。
2、它并不是內在線程安全的,并且使用起來比其他計時器更麻煩。此計時器通常不適合 Windows 窗體環境。
構造函數:public Timer(TimerCallback callback, object state, int dueTime, int period);
string state=”.”;
//state參數可以傳入想在callback委托中處理的對象??梢詡鬟f一個AutoRestEvent,在回調函數中向主函數發送信號。
Timer timer=new Timer(TimeMethod,state,100,1000)//100表示多久后開始,1000表示隔多久執行一次。
void TimerMethod(object state)
{Console.Write(state.ToString());}
timer.Dispose();//取消timer執行1、針對服務器的服務程序,基于System.Threading.Timer,被設計并優化成能用于多線程環境。在這種情況下,應該確保事件處理程序不與 UI 交互。在asp.net中一般使用System.Timers.Timer。
2、繼承自Compnent,公開了可以SynchronizingObject 屬性,避免了線程池中無法訪問主線程中組件的問題(模擬System.Windows.Forms.Timer單線程模式)。但是除非需要對事件的時間安排進行更精確的控制,否則還是應該改為使用 System.Windows.Forms.Timer。
3、AutoReset屬性設置計時器是否在引發Elapsed事件后重新計時,默認為true。如果該屬性設為False,則只執行timer_Elapsed方法一次。
4、System.Timers.Timer是多線程定時器,如果一個Timer沒有處理完成,到達下一個時間點,新的Timer同樣會被啟動。所以,Timer比較適合執行不太耗時的小任務,若在Timer中運行耗時任務,很容易出現由于超時導致的多線程重入問題,即多個線程同時進入timer_Elapsed方法。
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 500;
timer.SynchronizingObject = this;
timer.Elapsed+=new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start(); private void timer_Elapsed(Object source, Timers.ElapsedEventArgs e)
{
this.tbTimer.Text = value;
}5、為了應對多線程重入問題??梢约渔i,也可以增加標志位。 Interlocked.Exchange提供了一種輕量級的線程安全的給對象賦值的方法,所以使用Interlocked.Exchange給變量賦值。
int inTimer = 0;
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (Interlocked.Exchange(ref inTimer, 1) == 0)
{
Thread.Sleep(3000);
string currentThreadId = Thread.CurrentThread.ManagedThreadId.ToString();
this.Dispatcher.BeginInvoke(new Action(() =>
{
this.Label_Result.Content += currentThreadId + ",";
}), null);
Interlocked.Exchange(ref inTimer, 0);
}
}此計時器直接繼承自Component,它經過了專門的優化以便與 Windows 窗體一起使用,并且必須在窗口中使用。
Windows計時器建立在基于消息的UI線程上運行,精度限定為5ms。Tick事件中執行的事件與主窗體是同一個線程(單線程),并且對與 UI 交互是安全的。
只有Enable和Internal兩個屬性和一個Tick事件,可以使用Start()和Stop()方法控制Enable屬性。
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}");
};
}主要用于WPF中。屬性和方法與System.Windows.Forms.Timer類似。DispatcherTimer中Tick事件執行是在主線程中進行的。
使用DispatcherTimer時有一點需要注意,因為DispatcherTimer的Tick事件是排在Dispatcher隊列中的,當系統在高負荷時,不能保證在Interval時間段執行,可能會有輕微的延遲,但是絕對可以保證Tick的執行不會早于Interval設置的時間。如果對Tick執行時間準確性高可以設置DispatcherTimer的priority。
關于“C#四種計時器Timer怎么使用”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“C#四種計時器Timer怎么使用”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。