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