在winform中,使用PeekMessage处理完消息队列,使UI有机会更新。在WPF中,可以在Dispatch里使用PushFrame达到同样的效果。
public void DoEvents()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(delegate(object f)
{
((DispatcherFrame)f).Continue = false;
return null;
}
), frame);
Dispatcher.PushFrame(frame);
}
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(delegate(object f)
{
((DispatcherFrame)f).Continue = false;
return null;
}
), frame);
Dispatcher.PushFrame(frame);
}
写了这个方法后,我们在循环中或者事件中,在需要更新的UI后面调用一下DoEvents()就可以了。
以下内容需要引用 using System.Windows.Threading 命名空间