WPF: MessageBox.Show in Window.OnClosing crash on restore window


SUBMITTED BY: TheDuff10

DATE: Aug. 15, 2017, 2:22 p.m.

FORMAT: Text only

SIZE: 4.5 kB

HITS: 370

  1. public partial class MainWindow : Window
  2. {
  3. Window childWindow;
  4. public MainWindow()
  5. {
  6. InitializeComponent();
  7. Loaded += MainWindow_Loaded;
  8. }
  9. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  10. {
  11. childWindow = new Window();
  12. childWindow.Owner = this;
  13. childWindow.Closing += ChildWindow_Closing;
  14. childWindow.Show();
  15. }
  16. private void ChildWindow_Closing(object sender, CancelEventArgs e)
  17. {
  18. MessageBox.Show(childWindow, "Close child window?");
  19. }
  20. }
  21. System.InvalidOperationException occurred
  22. HResult=0x80131509
  23. Message=Cannot set Visibility to Visible or call Show, ShowDialog, Close, or WindowInteropHelper.EnsureHandle while a Window is closing.
  24. Source=PresentationFramework
  25. StackTrace:
  26. at System.Windows.Window.VerifyNotClosing()
  27. at System.Windows.Window.CoerceVisibility(DependencyObject d, Object value)
  28. at System.Windows.DependencyObject.ProcessCoerceValue(DependencyProperty dp, PropertyMetadata metadata, EntryIndex& entryIndex, Int32& targetIndex, EffectiveValueEntry& newEntry, EffectiveValueEntry& oldEntry, Object& oldValue, Object baseValue, Object controlValue, CoerceValueCallback coerceValueCallback, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, Boolean skipBaseValueChecks)
  29. at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
  30. at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
  31. at System.Windows.Window.UpdateVisibilityProperty(Visibility value)
  32. at System.Windows.Window.WmShowWindow(IntPtr wParam, IntPtr lParam)
  33. at System.Windows.Window.WindowFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
  34. at System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
  35. at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
  36. at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
  37. at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
  38. at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
  39. at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
  40. at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
  41. I found what causes the exception. Prior using the 'Show Desktop' function, there are two app windows opened in the taskbar. Subsequently, when trying to restore the app from the taskbar, the child window is no longer visible. It seems that Windows makes a judgement call, losing the closing child window in advance. So when you click on the main window thumbnail, the app tries to bring into view the parent window but is unable to set its visibility, whilst there is its child in the closing process.
  42. There may be few approaches to this, but hiding the child window while closing may be one of them. Alternatively, you could temporarily hide the main window instead, this also prevents the exception. The former solution gives the visibility back to the parent window, assuming the child window will be closed (if not set it to visible again), the latter leaves the child window the only visible window and it forces Windows to show its thumbnail in the taskbar.
  43. fix1
  44. childWindow.ShowDialog();
  45. fix2
  46. private void ChildWindow_Closing(object sender, CancelEventArgs e)
  47. {
  48. childWindow.Visibility = Visibility.Hidden;
  49. MessageBox.Show(childWindow, "Close child window?");
  50. }
  51. fix3
  52. private void ChildWindow_Closing(object sender, CancelEventArgs e)
  53. {
  54. Visibility = Visibility.Hidden;
  55. MessageBox.Show(childWindow, "Close child window?");
  56. Visibility = Visibility.Visible;
  57. }

comments powered by Disqus