




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第C#中的三種定時(shí)計(jì)時(shí)器Timer用法介紹在.NET中有三種計(jì)時(shí)器:
1、System.Windows.Forms命名空間下的Timer控件,它直接繼承自Componet。Timer控件只有綁定了Tick事件和設(shè)置Enabled=True后才會(huì)自動(dòng)計(jì)時(shí),停止計(jì)時(shí)可以用Stop()方法控制,通過Stop()停止之后,如果想重新計(jì)時(shí),可以用Start()方法來啟動(dòng)計(jì)時(shí)器。Timer控件和它所在的Form屬于同一個(gè)線程;2、System.Timers命名空間下的Timer類。System.Timers.Timer類:定義一個(gè)System.Timers.Timer對(duì)象,然后綁定Elapsed事件,通過Start()方法來啟動(dòng)計(jì)時(shí),通過Stop()方法或者Enable=false停止計(jì)時(shí)。AutoReset屬性設(shè)置是否重復(fù)計(jì)時(shí)(設(shè)置為false只執(zhí)行一次,設(shè)置為true可以多次執(zhí)行)。Elapsed事件綁定相當(dāng)于另開了一個(gè)線程,也就是說在Elapsed綁定的事件里不能訪問其它線程里的控件(需要定義委托,通過Invoke調(diào)用委托訪問其它線程里面的控件)。3、System.Threading.Timer類。定義該類時(shí),通過構(gòu)造函數(shù)進(jìn)行初始化。
在上面所述的三種計(jì)時(shí)器中,第一種計(jì)時(shí)器和它所在的Form處于同一個(gè)線程,因此執(zhí)行的效率不高;而第二種和第三種計(jì)時(shí)器執(zhí)行的方法都是新開一個(gè)線程,所以執(zhí)行效率比第一種計(jì)時(shí)器要好,因此在選擇計(jì)時(shí)器時(shí),建議使用第二種和第三種。
下面是三種定時(shí)器使用的例子:
1、Timer控件
設(shè)計(jì)界面:
后臺(tái)代碼:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceTimerDemo
publicpartialclassFrmMain:Form
//定義全局變量
publicintcurrentCount=0;
publicFrmMain()
InitializeComponent();
privatevoidFrmMain_Load(objectsender,EventArgse)
//設(shè)置Timer控件可用
this.timer.Enabled=true;
//設(shè)置時(shí)間間隔(毫秒為單位)
this.timer.Interval=1000;
privatevoidtimer_Tick(objectsender,EventArgse)
currentCount+=1;
this.txt_Count.Text=currentCount.ToString().Trim();
privatevoidbtn_Start_Click(objectsender,EventArgse)
//開始計(jì)時(shí)
this.timer.Start();
privatevoidbtn_Stop_Click(objectsender,EventArgse)
//停止計(jì)時(shí)
this.timer.Stop();
}
2、System.Timers.Timer
設(shè)計(jì)界面:
后臺(tái)代碼:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceTimersTimer
publicpartialclassFrmMain:Form
//定義全局變量
publicintcurrentCount=0;
//定義Timer類
System.Timers.Timertimer;
//定義委托
publicdelegatevoidSetControlValue(stringvalue);
publicFrmMain()
InitializeComponent();
privatevoidForm1_Load(objectsender,EventArgse)
InitTimer();
///summary
///初始化Timer控件
////summary
privatevoidInitTimer()
//設(shè)置定時(shí)間隔(毫秒為單位)
intinterval=1000;
timer=newSystem.Timers.Timer(interval);
//設(shè)置執(zhí)行一次(false)還是一直執(zhí)行(true)
timer.AutoReset=true;
//設(shè)置是否執(zhí)行System.Timers.Timer.Elapsed事件
timer.Enabled=true;
//綁定Elapsed事件
timer.Elapsed+=newSystem.Timers.ElapsedEventHandler(TimerUp);
///summary
///Timer類執(zhí)行定時(shí)到點(diǎn)事件
////summary
///paramname="sender"/param
///paramname="e"/param
privatevoidTimerUp(objectsender,System.Timers.ElapsedEventArgse)
currentCount+=1;
this.Invoke(newSetControlValue(SetTextBoxText),currentCount.ToString());
catch(Exceptionex)
MessageBox.Show("執(zhí)行定時(shí)到點(diǎn)事件失敗:"+ex.Message);
///summary
///設(shè)置文本框的值
////summary
///paramname="strValue"/param
privatevoidSetTextBoxText(stringstrValue)
this.txt_Count.Text=this.currentCount.ToString().Trim();
privatevoidbtn_Start_Click(objectsender,EventArgse)
timer.Start();
privatevoidbtn_Stop_Click(objectsender,EventArgse)
timer.Stop();
}
3、System.Threading.Timer
設(shè)計(jì)界面:
后臺(tái)代碼:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Threading;
namespaceThreading.Timer
publicpartialclassFrmMain:Form
//定義全局變量
publicintcurrentCount=0;
//定義Timer類
System.Threading.TimerthreadTimer;
//定義委托
publicdelegatevoidSetControlValue(objectvalue);
publicFrmMain()
InitializeComponent();
privatevoidFrmMain_Load(objectsender,EventArgse)
InitTimer();
///summary
///初始化Timer類
////summary
privatevoidInitTimer()
threadTimer=newSystem.Threading.Timer(newTimerCallback(TimerUp),null,Timeout.Infinite,1000);
///summary
///定時(shí)到點(diǎn)執(zhí)行的事件
////summary
///paramname="value"/param
privatevoidTimerUp(objectvalue)
currentCount+=1;
this.Invoke(newSetControlValue(SetTextBoxValue),currentCount);
///summary
///給文本框賦值
////summary
///paramname="value"/param
privatevoidSetTextBoxValue(objectvalue)
this.txt_Count.Text=value.ToString();
///summary
///開始
////summary
///paramname="sender"/param
///paramname="e"/param
privatevoidbtn_Start_Click(objectsender,EventArgse)
//立即開始計(jì)時(shí),時(shí)間間隔1000毫秒
threadTimer.Change(0,1000);
///summary
///停止
////summary
///paramname=
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 中國裝飾原紙市場動(dòng)態(tài)監(jiān)測及競爭戰(zhàn)略研究報(bào)告
- 2024年全球及中國自動(dòng)氣瓶架行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報(bào)告
- 兒童體育體驗(yàn)館策劃書3
- 廣告色膏行業(yè)深度研究分析報(bào)告(2024-2030版)
- 農(nóng)村土地制度改革對(duì)農(nóng)村集體經(jīng)濟(jì)發(fā)展的影響研究
- 2025年婦產(chǎn)科醫(yī)生述職報(bào)告例文(四)
- 2025年電網(wǎng)側(cè)儲(chǔ)能市場規(guī)模分析
- 江西省婺源縣2025屆七下生物期末調(diào)研試題含解析
- 2025年江蘇揚(yáng)州市儀征市十二圩新區(qū)建設(shè)有限公司招聘筆試參考題庫含答案解析
- 2025年廣西崇左憑祥市祥信城市建設(shè)有限責(zé)任公司招聘筆試參考題庫含答案解析
- 機(jī)泵類設(shè)備培訓(xùn)
- 大學(xué)生職業(yè)生涯規(guī)劃與就業(yè)創(chuàng)業(yè)指導(dǎo)(四川水利職業(yè)技術(shù)學(xué)院)知到智慧樹答案
- 山東師大附中2025屆高考英語一模試卷含解析
- 三管三必須-新安法宣貫課件
- 競聘醫(yī)院科室副主任
- JJF(浙) 1126-2016 風(fēng)速變送器校準(zhǔn)規(guī)范
- 七年級(jí)歷史下冊(cè) 第一單元 隋唐時(shí)期繁榮與開放的時(shí)代 第2課 從貞觀之治到開元盛世教學(xué)設(shè)計(jì)2 新人教版
- AI教育項(xiàng)目商業(yè)計(jì)劃書
- 電廠水化驗(yàn)員職業(yè)技能鑒定題庫(高級(jí)工)第001套
- 2024-2030年全球及中國銀離子抗菌敷料市場營銷渠道及應(yīng)用領(lǐng)域分析研究報(bào)告
- NB/T 11454-2023凍結(jié)法鑿井風(fēng)險(xiǎn)管理規(guī)范
評(píng)論
0/150
提交評(píng)論