WPF C#的定时器函数:

使用System.Timers.Timer类 
System.Timers.Timer t = new System.Timers.Timer(10000);//实例化Timer类,设置间隔时间为10000毫秒; 
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件; 
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); 
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; 
public void theout(object source, System.Timers.ElapsedEventArgs e) 
{ 
       MessageBox.Show("OK!"); 
}

4:System.Windows.Threading.DispatcherTimer(WPF timer);
使用:
private static System.Windows.Threading.DispatcherTimer readDataTimer = new System.Windows.Threading.DispatcherTimer();
readDataTimer.Tick += new EventHandler(timeCycle);
readDataTimer.Interval = new TimeSpan(0, 0, 0, 1);
readDataTimer.Start();

public static void timeCycle(object sender, EventArgs e)
{

}

需要注意的是在wpf中涉及到界面操作的话,一定要使用第四种定时器DispatcherTime,DispatcherTimer是为wpf专门设计的,不然的话会提示界面资源被其他线程所拥有而无法更新界面。

猜你喜欢

转载自blog.csdn.net/tiger15605353603/article/details/81406840