先如下定义一个定时器:
public DispatcherTimer dispatcherTimer;
然后在某处创建这个对象实例:
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.
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。