作为一名励志成为程序媛的小白,决定从今天开始洗心革面,好好做媛。
在做WPF相关项目时,常会用到定时器,整理如下:
1.首先,要用到DispatcherTimer类,需引用using System.Windows.Threading命名空间;
2.实例化timer
DispatcherTimer timer=new DispatcherTimer();
3.通过委托实现定时执行的事件
timer.Tick+=new EventHandler(timer_Tick);
4.通过上述委托会形成一个方法
private void timer_Tick(object sender , EventArgs e)
{
//写要定时执行的程序
}
5.定时执行时间的设定,此处设置0.1秒
timer.Interval=TimeSpan.FormSeconds(0.1);
6.将定时器打开
timer.start();
完成!!!棒棒哒