Timer定時(shí)器的設(shè)計(jì)方法-基礎(chǔ)電子_第1頁
Timer定時(shí)器的設(shè)計(jì)方法-基礎(chǔ)電子_第2頁
Timer定時(shí)器的設(shè)計(jì)方法-基礎(chǔ)電子_第3頁
Timer定時(shí)器的設(shè)計(jì)方法-基礎(chǔ)電子_第4頁
Timer定時(shí)器的設(shè)計(jì)方法-基礎(chǔ)電子_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

精品文檔-下載后可編輯Timer定時(shí)器的設(shè)計(jì)方法-基礎(chǔ)電子在系統(tǒng)開發(fā)過程中經(jīng)常用到定時(shí)器進(jìn)行定時(shí)處理,比如比較常見的郵件群發(fā)、實(shí)時(shí)更新論壇的在線人數(shù)、文章數(shù)、點(diǎn)擊率等。很多情況下,我們不能對(duì)某一狀態(tài)或者某一行為進(jìn)行實(shí)時(shí)監(jiān)控,所以就希望系統(tǒng)能夠?qū)崿F(xiàn)這一功能。通過多線程技術(shù)可以使得定時(shí)器的性能更高。盡管定時(shí)器能夠自動(dòng)處理或者一些批處理操作,但是定時(shí)器也給系統(tǒng)帶來一定的安全隱患,特別是當(dāng)定時(shí)進(jìn)行的操作出現(xiàn)bug時(shí),如果沒有對(duì)Exception做出及時(shí)的處理,系統(tǒng)資源將會(huì)大大的浪費(fèi),嚴(yán)重的情況下,可能導(dǎo)致系統(tǒng)崩潰。因此,對(duì)于定時(shí)器的使用一定要慎重,至少要保證定時(shí)處理的行為出現(xiàn)異常的可能性很小,并在出現(xiàn)Exception的情況下及時(shí)處理。System.Threading.Timer是一個(gè)非常常用的定時(shí)器類,是一個(gè)使用回調(diào)方法的計(jì)時(shí)器,而且由線程池線程服務(wù),簡單且對(duì)資源要求不高。publicTimer(TimerCallbackcallback,Objectstate,TimeSpandueTime,TimeSpanperiod)參數(shù)callback一個(gè)TimerCallback委托,表示要執(zhí)行的方法。state一個(gè)包含回調(diào)方法要使用的信息的對(duì)象,或者為空引用(在VisualBasic中為Nothing)。dueTimeTimeSpan,表示在callback參數(shù)調(diào)用它的方法之前延遲的時(shí)間量。指定-1毫秒以防止啟動(dòng)計(jì)時(shí)器。指定零(0)以立即啟動(dòng)計(jì)時(shí)器。period在調(diào)用callback所引用的方法之間的時(shí)間間隔。指定-1毫秒可以禁用定期終止。方法、原理使用TimerCallback委托指定希望Timer執(zhí)行的方法。計(jì)時(shí)器委托在構(gòu)造計(jì)時(shí)器時(shí)指定,并且不能更改。此方法不在創(chuàng)建計(jì)時(shí)器的線程上執(zhí)行,而是在系統(tǒng)提供的ThreadPool線程上執(zhí)行。創(chuàng)建計(jì)時(shí)器時(shí),可以指定在次執(zhí)行方法之前等待的時(shí)間量(截止時(shí)間)以及此后的執(zhí)行期間等待的時(shí)間量(時(shí)間周期)??梢允褂肅hange方法更改這些值或禁用計(jì)時(shí)器。當(dāng)不再需要計(jì)時(shí)器時(shí),請(qǐng)使用Dispose方法釋放計(jì)時(shí)器持有的資源。如果希望在計(jì)時(shí)器被釋放時(shí)接收到信號(hào),請(qǐng)使用接受WaitHandle的Dispose(WaitHandle)方法重載。計(jì)時(shí)器已被釋放后,WaitHandle便終止。由計(jì)時(shí)器執(zhí)行的回調(diào)方法應(yīng)該是可重入的,因?yàn)樗窃赥hreadPool線程上調(diào)用的。備注:在超過dueTime以后及此后每隔period時(shí)間間隔,都會(huì)調(diào)用callback參數(shù)所指定的委托。如果dueTime為零(0),則立即調(diào)用callback。如果dueTime是-1毫秒,則不會(huì)調(diào)用callback;計(jì)時(shí)器將被禁用,但通過調(diào)用Change方法可以重新啟用計(jì)時(shí)器。如果period為零(0)或-1毫秒,而且dueTime為正,則只會(huì)調(diào)用callback;計(jì)時(shí)器的定期行為將被禁用,但通過使用Change方法可以重新啟用該行為。簡單的定時(shí)器usingSystem;usingSystem.Threading;publicclassTestTimer{/**////summary///定時(shí)器////summaryprivateTimeriTimer;/**////summary///constructor////summarypublicTestTimer(){iTimer=newSystem.Threading.Timer(newTimerCallback(Doing));iTimer.Change(TimeSpan.FromMinutes(5),TimeSpan.FromMinutes(5));}/**////summary///////summary///paramname="nObject"/parampublicvoidDoing(objectnObject){//dosomething}}一個(gè)比較完整的計(jì)時(shí)器:下面是我設(shè)計(jì)的一個(gè)簡單實(shí)例。在一個(gè)問卷調(diào)查系統(tǒng)中,每一張問卷都有其終止日期,當(dāng)?shù)竭_(dá)了終止日期時(shí),需要系統(tǒng)自動(dòng)將其關(guān)閉。這就需要定時(shí)器對(duì)問卷的狀態(tài)和終止日期進(jìn)行實(shí)時(shí)監(jiān)控,及時(shí)關(guān)閉。這里采用了一個(gè)簡單的單件模式來操作、控制定時(shí)器。這里主要的操作包括定時(shí)器開始、終止、執(zhí)行。PaperManager管理類/**////summary///管理類////summarypublicclassPaperManager{/**////summary///定時(shí)器////summaryprivateTimeriTimer;/**////summary///啟動(dòng)時(shí)間////summaryprivateTimeSpandueTime;/**////summary///方法調(diào)用間隔////summaryprivateTimeSpanperiod;/**////summary///委托////summaryprivateTimerCallbacktimerDelegate;/**////summary///靜態(tài)實(shí)例////summaryprivatestaticreadonlyPaperManagerself=newPaperManager();/**////summary///構(gòu)造函數(shù)////summarypublicPaperManager(){timerDelegate=newTimerCallback(CheckStatus);}/**////summary///////summary///returns/returnspublicstaticPaperManagergetInstance(){returnself;}/**////summary///設(shè)置啟動(dòng)時(shí)間間隔////summary///paramname="days"天/param///paramname="hours"小時(shí)/param///paramname="minutes"分鐘/param///paramname="seconds"秒/param///paramname="milisecond"毫秒/parampublicvoidsetDueTime(intdays,inthours,intminutes,intseconds,intmilisecond){dueTime=newTimeSpan(days,hours,minutes,seconds,milisecond);}/**////summary///設(shè)置回調(diào)時(shí)間間隔

////summary///paramname="days"天/param///paramname="hours"小時(shí)/param///paramname="minutes"分鐘/param///paramname="seconds"秒/param///paramname="milisecond"毫秒/parampublicvoidsetPeriod(intdays,inthours,intminutes,intseconds,intmilisecond){period=newTimeSpan(days,hours,minutes,seconds,milisecond);}/**////summary///開始////summarypublicvoidStart(){AutoResetEventautoEvent=newAutoResetEvent(false);dueTime=TimeSpan.FromSeconds(0);period=TimeSpan.FromSeconds(10);iTimer=newTimer(timerDelegate,autoEvent,dueTime,period);autoEvent.WaitOne(5000,false);iTimer.Change(dueTime,period);}/**////summary///停止////summarypublicvoidStop(){iTimer.Dispose();}/**////summary///執(zhí)行////summarypublicvoidExcuteOneTime(){if(iTimer!=null){iTimer.Dispose();}//如果period為零(0)或-1毫秒,而且dueTime為正,則只會(huì)調(diào)用callback;//計(jì)時(shí)器的定期行為將被禁用,但通過使用Change方法可以重新啟用該行為。setDueTime(0,0,0,0,1);setPeriod(0,0,0,0,-1);AutoResetEventautoEvent=newAutoResetEvent(false);iTimer=newTimer(timerDelegate,autoEvent,dueTime,period);autoEvent.WaitOne(5000,false);iTimer.Change(dueTime,period);}/**////summary///行為////summary///paramname="nObject"/parampublicvoidCheckStatus(objectnObject){AutoResetEventautoEvent=(AutoResetEvent)nObject;if(ExcuteUpdate()){autoEvent.Set();}}/**////summary///更新////summary///returns/returnsprivateboolExcuteUpdate(){try{//應(yīng)該從數(shù)據(jù)庫獲得Paper對(duì)象的集合,這里簡略//ListPaperpaperList=getPaperList();ListPaperpaperList=newListPaper();foreach(PaperiteminpaperList){if(item.EndTime=DateTime.Now){if(item.Status==Paper.StatusOfNormal){item.Status=Paper.StatusOfTerminate;}}}/**/////執(zhí)行數(shù)據(jù)更新,這里省略returntrue;}catch{returnfalse;}}}這是問卷的實(shí)體類,只是簡單的列出必要的屬性。Paper實(shí)體類/**////summary///實(shí)體類////summarypublicclassPaper{/**////summary///終止時(shí)間////summarypublicDateTimeEndTime;/**////summary///狀態(tài)////summarypublicintStatus;/**////summary///正常////summarypublicconstintStatusOfNormal

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論