简体   繁体   中英

Exception in ShowDialog from different Window

I have a custom Window I want to show, when an unhandled exception occurs. Something like this:

private void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    try
    {
        e.Handled = true; // If e.Handled == false or error dialog will not show.
        string errorMassage = ExceptionFormatter.FormatException(e.Exception)
        var window = new FatalErrorWindow(errorMassage );
        window.ShowDialog();
    }
    catch (Exception ex)
    {
        MessageBox.Show(errorMassage, "Program error", MessageBoxButton.OK);            
    }
    Application.Current.Shutdown(-1);
}

In some situations the ShowDialog itself throws an exception, so I added a try/catch for that, and discovered that when ShowDialog is called, it seems as if .NET performs a full UpdateLayout cycle for a different window (the main window presumably?).

Now the problem is that if that cycle throws an exception during the Measure/Arrange phase, that is basically not possible to handle gracefully. Any attempt to show a pretty dialog will run into the same exception.

Does anyone have a suggestion how to fix/get around this? ¨

EDIT: (In response to comment)

My concern is that I cannot try/catch anything to prevent this. ANY UI element - that being my own, a 3rd party or even a .NET element - in a completely unrelated part of the application, could throw a NullReferenceException in a Measure/Arrange method, and it is essentially uncatchable.

Below is a stacktrace which illustrates the problem.

at 3rdPartyVendor.UI.Xaml.Grid.GridDataRow.UpdateFixedRowState()
at 3rdPartyVendor.UI.Xaml.Grid.RowGenerator.EnsureRows(VisibleLinesCollection visibleRows) at 3rdPartyVendor.UI.Xaml.Grid.VisualContainer.EnsureItems(Boolean ensureColumns) at 3rdPartyVendor.UI.Xaml.Grid.VisualContainer.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) in f:\\dd\\wpf\\src\\Framework\\MS\\Internal\\Helper.cs:line 547 at System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Primitives\\ScrollContentPresenter.cs:line 432 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1431
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1286
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1250
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 701 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\ScrollViewer.cs:line 1220 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Border.cs:line 245
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1431
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1286
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1250
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 701 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Control.cs:line 711
at 3rdPartyVendor.UI.Xaml.Grid.SfDataGrid.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1431
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1286
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1250
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 701 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) in f:\\dd\\wpf\\src\\Framework\\MS\\Internal\\Helper.cs:line 547 at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\ContentPresenter.cs:line 495 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Control.cs:line 711
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at XXXXXX.Common.Controls.CachingPanel.MeasureOverride(Size availableSize) in F:\\git\\XXXXXXX\\Source\\XXXXXX.Common\\Controls\\CachingPanel.cs:line 84
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 441 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Border.cs:line 245
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Control.cs:line 711
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) in f:\\dd\\wpf\\src\\Framework\\MS\\Internal\\Helper.cs:line 547 at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\ContentPresenter.cs:line 495 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 441 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1431
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1286
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1250
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 701 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) in f:\\dd\\wpf\\src\\Framework\\MS\\Internal\\Helper.cs:line 547 at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\ContentPresenter.cs:line 495 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 441 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Control.cs:line 711
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Control.cs:line 711
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) in f:\\dd\\wpf\\src\\Framework\\MS\\Internal\\Helper.cs:line 547 at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\ContentPresenter.cs:line 495 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Border.cs:line 245
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1431
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1286
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1250
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 701 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 441 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Control.cs:line 711
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) in f:\\dd\\wpf\\src\\Framework\\MS\\Internal\\Helper.cs:line 547 at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\ContentPresenter.cs:line 495 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Control.cs:line 711
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at XXXXXX.Common.Controls.CachingPanel.MeasureOverride(Size availableSize) in F:\\git\\XXXXXXX\\Source\\XXXXXX.Common\\Controls\\CachingPanel.cs:line 84
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1431
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1286
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1250
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 701 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) in f:\\dd\\wpf\\src\\Framework\\MS\\Internal\\Helper.cs:line 547 at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\ContentPresenter.cs:line 495 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Control.cs:line 711
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at XXXXXX.Common.Controls.CachingPanel.MeasureOverride(Size availableSize) in F:\\git\\XXXXXXX\\Source\\XXXXXX.Common\\Controls\\CachingPanel.cs:line 84
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 441 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Border.cs:line 245
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Control.cs:line 711
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 441 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1431
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1286
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 1250
at System.Windows.Controls.Grid.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Grid.cs:line 701 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) in f:\\dd\\wpf\\src\\Framework\\MS\\Internal\\Helper.cs:line 547 at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\ContentPresenter.cs:line 495 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Documents\\AdornerDecorator.cs:line 129 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Controls\\Border.cs:line 245
at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4402
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Window.MeasureOverrideHelper(Size constraint) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 3497 at System.Windows.Window.MeasureOverride(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 1881 at System.Windows.FrameworkElement.MeasureCore(Size availableSize) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\FrameworkElement.cs:line 4323
at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.ContextLayoutManager.UpdateLayout() at System.Windows.UIElement.UpdateLayout() at System.Windows.Interop.HwndSource.SetLayoutSize() at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value) at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at System.Windows.Window.SetRootVisual() in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 2967 at System.Windows.Window.SetRootVisualAndUpdateSTC() in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 2978 at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 2942 at System.Windows.Window.CreateSourceWindow(Boolean duringShow) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 2679 at System.Windows.Window.CreateSourceWindowDuringShow() in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 2532 at System.Windows.Window.SafeCreateWindowDuringShow() in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 5643 at System.Windows.Window.ShowHelper(Object booleanBox) in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 5743 at System.Windows.Window.Show() in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 214 at System.Windows .Window.ShowDialog() in f:\\dd\\wpf\\src\\Framework\\System\\Windows\\Window.cs:line 516 at XXXXXX.Common.Controls.MultiButtonDialog.ShowOK(Object content, String title) in F:\\git\\XXXXXXX\\Source\\XXXXXX.Common\\Dialogs\\MultiButtonDialog.xaml.cs:line 55 at XXXXXX.Wpf.VelocityErrorHandler.<>c__DisplayClass17_0.b__0() in F:\\git\\XXXXXXX\\Source\\XXXXXX.Wpf\\VelocityFatalErrorHandler.cs:line 216 at XXXXXX.Wpf.WpfDispatcher.Invoke(Action action) in F:\\git\\XXXXXXX\\Source\\XXXXXX.Wpf\\WpfDispatcher.cs:line 32 at DFDS.Freight.Common.Core.DispatchContext.Invoke(Action action) in F:\\git\\XXXXXXX\\Source\\DFDS.Freight.Common.Portable\\Core\\IDispatchContext.cs:line 30 at XXXXXX.Wpf.VelocityErrorHandler.HandleFatalException(Exception exception) in F:\\git\\XXXXXXX\\Source\\XXXXXX.Wpf\\VelocityFatalErrorHandler.cs:line 206 at XXXXXX.Wpf.VelocityErrorHandler.HandleAnyException(Exception exception, Boolean isUnhandledTaskException) in F:\\git\\XXXXXXX\\Source\\XXXXXX.Wpf\\VelocityFatalErrorHandler.cs:line 113 at XXXXXX.Wpf.Veloci tyErrorHandler.HandleUnhandledException(Exception exception, Boolean isUnhandledTaskException) in F:\\git\\XXXXXXX\\Source\\XXXXXX.Wpf\\VelocityFatalErrorHandler.cs:line 29 at XXXXXX.Wpf.ApplicationInitializer.OnDispatcherUnhandledException(Object sender, DispatcherUnhandledExceptionEventArgs e) in F:\\git\\XXXXXXX\\Source\\XXXXXX.Wpf\\ApplicationInitializer.cs:line 605
at System.Windows.Threading.Dispatcher.CatchException(Exception e)
at System.Windows.Threading.Dispatcher.CatchExceptionStatic(Object source, Exception e) at System.Windows.Threading.ExceptionWrapper.CatchException(Object source, Exception e, Delegate catchHandler) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at MS.Internal.CulturePreser vingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

I believe your solution is in fact as good as it gets. You cannot guarantee a stable state when you end up in the DispatcherUnhandledException event handler. For instance, if your exceptions is thrown from deep inside WPF you will likely not be able to bind properties. Your error message dialog would be blank.

Below you find a fix for that particular issue.

private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
    var errorMassage = ExceptionFormatter.FormatException(e.Exception)

    try
    {            
        // When the WPF framework throws, our error dialog will have problem binding data.
        // In this case, we must revert to an oldschool message dialog in order to see
        // the actual error message!
        var isFrameworkError = ex.Source?.Equals("PresentationFramework") == true;
        if (isFrameworkError)
        {
            MessageBox.Show(errorMassage, "Program error", MessageBoxButton.OK);
        }
        else
        {
            var window = new FatalErrorWindow(errorMassage );
            window.ShowDialog();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(errorMassage, "Program error", MessageBoxButton.OK);
    }

    Application.Current.Shutdown(-1);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM