C#中的三種定時(shí)計(jì)時(shí)器Timer用法介紹_第1頁
C#中的三種定時(shí)計(jì)時(shí)器Timer用法介紹_第2頁
C#中的三種定時(shí)計(jì)時(shí)器Timer用法介紹_第3頁
C#中的三種定時(shí)計(jì)時(shí)器Timer用法介紹_第4頁
C#中的三種定時(shí)計(jì)時(shí)器Timer用法介紹_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

最新文檔

評(píng)論

0/150

提交評(píng)論