新聞中心
先如下定義一個(gè)定時(shí)器:
為黃山等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及黃山網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、黃山網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
public DispatcherTimer dispatcherTimer;
然后在某處創(chuàng)建這個(gè)對(duì)象實(shí)例:
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
設(shè)定超時(shí)回調(diào)函數(shù):
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
設(shè)定間隔(下方例子是10秒鐘):
dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
啟動(dòng)定時(shí)器:
dispatcherTimer.Start();
定義超時(shí)回調(diào)函數(shù):
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// do something here...
}
值得注意的是DispatcherTimer是無(wú)法直接在console下運(yùn)行的,需要額外的東西。
下面文字來(lái)自http://stackoverflow.com/questions/19351473/dispatchertimer-doesnt-work-in-console的解釋
The console and unit test environment by default don't have a dispatcher to run your dispatcher timer.
You can still use Dispatcher.CurrentDispatcher to create a Dispatcher to run your code.
分享文章:c#之下如何做定時(shí)
URL鏈接:http://fisionsoft.com.cn/article/pojgej.html