wpf 让正执行的程序暂停几秒钟

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();//这里就可以调用了
posted @ 2017-11-15 17:00  liu_xh  阅读(1263)  评论(0编辑  收藏  举报