C#中四種定時(shí)器的用法詳解
更新時(shí)間:2024年04月17日 10:27:27 作者:※※冰馨※※
日常項(xiàng)目開(kāi)發(fā)中,很多時(shí)候都需要用到定時(shí)器來(lái)處理一些問(wèn)題,那么c#中各種定時(shí)器應(yīng)該怎么用呢?下面來(lái)簡(jiǎn)單介紹下C#中4種定時(shí)器的使用方法說(shuō)明,感興趣的朋友可以參考下
C#四種定時(shí)器的用法
- 1. System.Windows.Forms.Timer
- 2. System.Threading.Timer
- 3. System.Timers.Timer
- 4. System.Windows.Threading.DispatcherTimer(WPF中的定時(shí)器)
1. System.Windows.Forms.Timer
使用方法如下:
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();//創(chuàng)建定時(shí)器
timer.Tick += new EventHandler(timer1_Tick);//事件處理
timer.Enabled = true;//設(shè)置啟用定時(shí)器
timer.Interval = 1000;//執(zhí)行時(shí)間
timer.Start();//開(kāi)啟定時(shí)器
/// <summary>
/// 定時(shí)器事件處理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
timer.Stop();//停止定時(shí)器
timer.Tick -= new EventHandler(timer1_Tick);//取消事件
timer.Enabled = false;//設(shè)置禁用定時(shí)器
}2. System.Threading.Timer
使用方法如下:
System.Threading.Timer timer;
timer = new System.Threading.Timer(new TimerCallback(timerCall), this, 3000, 0);//創(chuàng)建定時(shí)器
/// <summary>
/// 事件處理
/// </summary>
/// <param name="obj"></param>
private void timerCall(object obj)
{
timer.Dispose();//釋放定時(shí)器
} 3. System.Timers.Timer
使用方法如下:
System.Timers.Timer timer = new System.Timers.Timer(1000);//創(chuàng)建定時(shí)器,設(shè)置間隔時(shí)間為1000毫秒;
timer.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到達(dá)時(shí)間的時(shí)候執(zhí)行事件;
timer.AutoReset = true;//設(shè)置是執(zhí)行一次(false)還是一直執(zhí)行(true);
timer.Enabled = true;//需要調(diào)用 timer.Start()或者timer.Enabled = true來(lái)啟動(dòng)它,
timer.Start();//timer.Start()的內(nèi)部原理還是設(shè)置timer.Enabled = true;
/// <summary>
///執(zhí)行事件
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
public void theout(object source, System.Timers.ElapsedEventArgs e)
{
timer.Elapsed -= new System.Timers.ElapsedEventHandler(theout); //取消執(zhí)行事件;
timer.Enabled = false;//禁用
timer.Stop();//停止
}4. System.Windows.Threading.DispatcherTimer(WPF中的定時(shí)器)
使用方法如下:
private static System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();//創(chuàng)建定時(shí)器
timer.Tick += new EventHandler(timer_out);//執(zhí)行事件
timer.Interval = new TimeSpan(0, 0, 0, 1);//1s執(zhí)行
timer.IsEnabled = true;//啟用
timer.Start();//開(kāi)啟
/// <summary>
///執(zhí)行事件
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
public static void timer_out(object sender, EventArgs e)
{
timer.Tick -= new EventHandler(timer_out);//取消執(zhí)行事件;
timer.IsEnabled = false;//禁用
timer.Stop();//停止
}注意事項(xiàng)
- 1.DispatcherTimer是運(yùn)行在UI線程上的,其好處是可以在定時(shí)事件中修改UI元素,其它三種定時(shí)器是運(yùn)行在獨(dú)立的線程上的,與UI線程無(wú)關(guān)。
- 2.如果需要修改UI控件,則必須委托給調(diào)度器this.Dispatcher進(jìn)行,不然的話會(huì)提示界面資源被其他線程所擁有而無(wú)法更新界面。
以上就是C#中四種定時(shí)器的用法詳解的詳細(xì)內(nèi)容,更多關(guān)于C#定時(shí)器用法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Unity實(shí)現(xiàn)鼠標(biāo)雙擊與長(zhǎng)按的檢測(cè)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)鼠標(biāo)雙擊與長(zhǎng)按的檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
C#中ListView控件實(shí)現(xiàn)窗體代碼
這篇文章主要介紹了C#中ListView控件實(shí)現(xiàn)窗體的核心代碼,非常不錯(cuò),具有參考借鑒價(jià)值,對(duì)c#listview相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-08-08
C#開(kāi)發(fā)的人臉左右相似度計(jì)算軟件源碼分析
這篇文章主要介紹了C#開(kāi)發(fā)的人臉左右相似度計(jì)算軟件,較為詳細(xì)的分析了相似度計(jì)算的相關(guān)原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-04-04
Unity的BuildPlayerProcessor實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity的BuildPlayerProcessor實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
c#使用簡(jiǎn)單工廠模式實(shí)現(xiàn)生成html文件的封裝類(lèi)分享
這篇文章主要介紹了運(yùn)用了簡(jiǎn)單工廠模式實(shí)現(xiàn)頁(yè)面靜態(tài)化封裝類(lèi),思路比較簡(jiǎn)單,大家可根據(jù)自己的思路再擴(kuò)展此類(lèi)2014-01-01

