【问题标题】:Dispatcher.DisableProcessing - InvalidOperationExceptionDispatcher.DisableProcessing - InvalidOperationException
【发布时间】:2014-12-18 09:15:01
【问题描述】:

我正在研究 Dispatcher.DisableProcessing() 作为暂停和恢复 UI 渲染的一种方式,并且我试图在没有 using(){} 语句的情况下使用它。

它抛出 InvalidOperationException - Dispatcher 处理已暂停,但消息仍在处理中。

问题: 为什么会抛出这个异常? 有什么方法可以在 using(){} 语句之外使用它?

提前致谢!

-已编辑以包含测试代码-

工作流程:

成功运行 OnPauseButtonClick 代码后仍然会发生该异常。

public class MainWindow: Window
{
    public MainWindow()
    {
        Initialize();
    }

    private DispatcherProcessingDisabled x;

    public void OnPauseButtonClick(object sender, RoutedEventArgs e)
    {
        x = Dispatcher.DisableProcessing();
    }

    public void OnTestButtonClick(object sender, RoutedEventArgs e)
    {
        Timer t = new Timer(TimerCallback, null, 0, 1000);
    }

    public void TimerCallback(object o)
    {
        //all codes were commented out
    }
}

【问题讨论】:

    标签: c# .net wpf dispatcher


    【解决方案1】:

    好的,所以我找到了 Dispatcher 抛出异常的位置。

        [UIPermissionAttribute(SecurityAction.LinkDemand,Unrestricted=true)]
        [SecurityCritical]
        public static void PushFrame(DispatcherFrame frame)
        {
            if(frame == null)
            {
                throw new ArgumentNullException("frame");
            }
    
            Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
            if(dispatcher._hasShutdownFinished) // Dispatcher thread - no lock needed for read
            {
                throw new InvalidOperationException(SR.Get(SRID.DispatcherHasShutdown));
            }
    
            if(frame.Dispatcher != dispatcher)
            {
                throw new InvalidOperationException(SR.Get(SRID.MismatchedDispatchers));
            }
            //here
            if(dispatcher._disableProcessingCount > 0) 
            {
                throw new InvalidOperationException(SR.Get(SRID.DispatcherProcessingDisabled));
            }
    
            dispatcher.PushFrameImpl(frame);
        }
    

    Here

    根据stacktrace,这个函数是由Application.RunInternal调用的。该应用程序将一个工作推送到 Dispatcher 中,但由于它被禁用而无法执行此操作。我的猜测是,这项工作是按下按钮的效果,然后是按钮聚焦时的发光效果,但我不能确定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 2014-09-01
      • 2015-08-27
      • 2016-03-28
      相关资源
      最近更新 更多