【问题标题】:Under what circumstances could UIElement.UpdateLayout() call Environment.FailFast?UIElement.UpdateLayout()在什么情况下可以调用Environment.FailFast?
【发布时间】:2012-05-25 15:26:28
【问题描述】:

所以我有一个控件,它响应它的项目被更改,调用 UpdateLayout()。这导致堆栈跟踪如下所示:

描述:应用程序请求进程终止通过 System.Environment.FailFast(字符串消息)。消息:不可恢复 系统错误。堆栈:在 System.Environment.FailFast(System.String) 在 MS.Internal.Invariant.FailFast(System.String, System.String) 在 MS.Internal.Invariant.Assert(Boolean, System.String) 在 System.Windows.Window.GetWindowMinMax() 在 System.Windows.Window.MeasureOverride(System.Windows.Size) 在 System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) 在 System.Windows.UIElement.Measure(System.Windows.Size) 在 System.Windows.ContextLayoutManager.UpdateLayout() 在 System.Windows.UIElement.UpdateLayout() 在

显然,在某些情况下,GetWindowMinMax() 会导致某种 Assert() 失败,从而导致调用 Environment.FailFast。在调用 UpdateLayout 之前,我可以检查哪些条件以确保不会出现这些情况以避免遇到此错误?

【问题讨论】:

    标签: .net wpf error-handling


    【解决方案1】:

    使用 Reflector 并查看GetWindowMinMax 的代码。这是断言:

    Invariant.Assert (!this.IsCompositionTargetInvalid, 
        "IsCompositionTargetInvalid is supposed to be false here") ;
    

    所以看起来您的窗口还没有真正创建为 Win32 窗口,或者它的 Win32 窗口已经被销毁。

    【讨论】:

    • 这似乎是真的;但是,如何解决在 UpdateLayout 运行之前检查窗口未关闭和有人关闭窗口之间的竞争条件?
    • 对 UpdateLayout 的调用是在哪里进行的?你能提供更多的堆栈跟踪吗?
    【解决方案2】:

    我对此有一致的再现:

            hwndSource = new System.Windows.Interop.HwndSource(p);
            this.Visibility = System.Windows.Visibility.Hidden;
            hwndSource.RootVisual = this;
    

    将 RootVisual 设置为隐藏窗口将触发 FailFast。

    【讨论】:

      猜你喜欢
      • 2017-11-30
      • 2012-03-02
      • 2011-07-08
      • 2012-10-21
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      相关资源
      最近更新 更多