2012-05-25 31 views
2

所以我有一个控件,它响应于它的项目被改变,调用UpdateLayout()。这导致堆栈跟踪如下所示:在什么情况下UIElement.UpdateLayout()可以调用Environment.FailFast?

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

显然,在某些情况下,GetWindowMinMax()是辉ling某种类型的Assert(),这会导致对Environment.FailFast的调用。在调用UpdateLayout之前可以检查哪些条件以确保不会发生这些情况以避免遇到此错误?

回答

2

以反射器并查看代码GetWindowMinMax。这是断言:

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

所以这样看来,是不是真的如一个Win32窗口中创建你的窗口又或者已经摧毁了Win32的窗口。

+0

这似乎是真的;然而,你如何解决检查窗口不关闭和在UpdateLayout运行前关闭窗口之间的竞争条件? – GWLlosa

+0

这个调用UpdateLayout的地方在哪里?你能提供更多的堆栈跟踪吗? –

2

我对此有一个一致的摄制:

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

设置RootVisual到隐藏的窗口将触发故障快速防护。

相关问题