網站首頁 編程語言 正文
1、System.Threading.Timer 線程計時器
1、最底層、輕量級的計時器。基于線程池實現的,工作在輔助線程。
2、它并不是內在線程安全的,并且使用起來比其他計時器更麻煩。此計時器通常不適合 Windows 窗體環境。
構造函數:public Timer(TimerCallback callback, object state, int dueTime, int period);
string state=”.”;
//state參數可以傳入想在callback委托中處理的對象。可以傳遞一個AutoRestEvent,在回調函數中向主函數發送信號。
Timer timer=new Timer(TimeMethod,state,100,1000)//100表示多久后開始,1000表示隔多久執行一次。
void TimerMethod(object state)
{Console.Write(state.ToString());}
timer.Dispose();//取消timer執行
2、System.Timers.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、為了應對多線程重入問題。可以加鎖,也可以增加標志位。 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);
}
}
3、System.Windows.Forms.Timer? Windows計時器
此計時器直接繼承自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}");
};
}
4. System.Windows.Threading.DispatcherTimer
主要用于WPF中。屬性和方法與System.Windows.Forms.Timer類似。DispatcherTimer中Tick事件執行是在主線程中進行的。
使用DispatcherTimer時有一點需要注意,因為DispatcherTimer的Tick事件是排在Dispatcher隊列中的,當系統在高負荷時,不能保證在Interval時間段執行,可能會有輕微的延遲,但是絕對可以保證Tick的執行不會早于Interval設置的時間。如果對Tick執行時間準確性高可以設置DispatcherTimer的priority。
原文鏈接:https://www.cnblogs.com/springsnow/p/9435696.html
相關推薦
- 2022-04-25 C語言的結構體你了解嗎_C 語言
- 2022-07-21 數據庫表數據操作-新增、刪除、修改
- 2022-03-15 has been blocked by CORS policy: Response to prefl
- 2023-08-28 react antd常見報錯Each child in a list should have a u
- 2022-12-12 Kotlin構造函數與成員變量和init代碼塊執行順序詳細講解_Android
- 2022-05-10 Element-ui 中的 Select 組件用(深度)選擇器修改默認樣式不生效的問題及如何使用 p
- 2022-09-15 C/C++?左移<<,?右移>>的作用及說明_C 語言
- 2022-11-05 安裝ingress-nginx遇到的一些坑實戰記錄_云其它
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支