先如下定義一個定時器:
public DispatcherTimer dispatcherTimer;
然后在某處創(chuàng)建這個對象實例:
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
設定超時回調函數:
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
設定間隔(下方例子是10秒鐘):
dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
啟動定時器:
dispatcherTimer.Start();
定義超時回調函數:
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// do something here...
}
值得注意的是DispatcherTimer是無法直接在console下運行的,需要額外的東西。
下面文字來自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.
另外有需要云服務器可以了解下創(chuàng)新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。