System.InvalidOperationException:暂停调度程序处理时无法执行此操作

时间:2015-12-16 06:37:48

标签: wpf dispatcher suspend

    public static void Dispatch(Action action)
    {

        var actionResult = action.BeginInvoke(null, null);

        while (!actionResult.IsCompleted)
        {
            FlushEventQueue();                
            Thread.Sleep(DispatcherHelper.WaitInternal);
        }
        action.EndInvoke(actionResult);
    }

    //Flush Event
    internal static void FlushEventQueue()
    {
        try
        {
            Dispatcher dispatcher = WpfUtils.GetDispatcher();
            dispatcher.Invoke(DispatcherPriority.Background, DispatcherHelper.FlushAction);

        }
        catch (Exception ex) 
        {
            Logger.Error(ex);
            throw;
        }
    }

    //Get Dispatcher 
    public static Dispatcher GetDispatcher()
    {  
        //set Dispatcher on Application OnStartUp event
        // AppDomain.CurrentDomain.SetData("Dispatcher", this.Dispatcher);
        Dispatcher dispatcher = AppDomain.CurrentDomain.GetData("Dispatcher") as Dispatcher;
        if (dispatcher == null)
        {
            dispatcher = Dispatcher.CurrentDispatcher;
        }

        return dispatcher;
    }

我遇到了一个例外:

  

System.InvalidOperationException:无法执行此操作   调度程序处理暂停。在   System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)
  在System.Windows.Threading.DispatcherOperation.Wait(TimeSpan超时)   在System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherOperation   operation,CancellationToken cancellationToken,TimeSpan timeout)
  在   System.Windows.Threading.Dispatcher.LegacyInvokeImpl(的DispatcherPriority   优先级,TimeSpan超时,委托方法,对象args,Int32   在Mvvm.Dispatching.DispatcherHelper.FlushEventQueue()的numArgs)   在Mvvm.Dispatching.DispatcherHelper.Dispatch(行动)

0 个答案:

没有答案