public static class DispatcherHelper
{
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static void DoEvents()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrames), frame);
try { Dispatcher.PushFrame(frame); }
catch (InvalidOperationException) { }
}
private static object ExitFrames(object frame)
{
((DispatcherFrame)frame).Continue = false;
return null;
}
}
在需要暂停的地方调用即可:DispatcherHelper.DoEvents();
如下面的示例:
//让它暂停3秒种
var t = DateTime.Now.AddMilliseconds(3000);
while (DateTime.Now < t)
DispatcherHelper.DoEvents();//这里就可以调用了