在WPF中,由于界面更新的复杂性,有时我需要在短暂的延迟后执行操作。
目前,我只是这样做:
var dt = new DispatcherTimer(DispatcherPriority.Send);
dt.Tick += (s, e) =>
{
dt.Stop();
//DoStuff
};
dt.Interval = TimeSpan.FromMilliseconds(200);
dt.Start();
但是每次创建一个新的计时器可能有些丑陋,而且可能会造成过多开销。从性能角度来看,最好的解决方案是什么?并且重写上述代码的好方法是什么,比如:
this.Dispatcher.BeginInvoke(new Action(delegate()
{
//DoStuff
}), DispatcherPriority.Send,TimeSpan.FromMilliseconds(200));
当Timespan表示延迟时间时,谢谢您的任何意见 :)