1

I am facing the same problem as described here:

Dispatcher throws InvalidOperationException on Messagebox.Show in Textchanged event

Although the solution works for me, I want to solve another problem: When I have no control on whether a message box is shown (external code), how do I gracefully recover from it?

If I set e.Handled = false an exception propagates thought the rest of the code until it is caught somewhere else it making some trivial warning or informational message box mess up the rest of the application.

On the other hand if I set e.Handled = true the dispatcher unhandled exception handler is called over and over again until I press "Ok" in the message box, at which point the application stays "frozen". Pressing pause, this is the call stack: enter image description here

New: Pressing any key with the (window focused) after closing the MessageBox causes the application to unfreeze and continue execution as normal.

How do I cause the MessageBox to be closed without breaking my application? (even if the messagebox is not shown at all)

EDIT
As requested here is the exception stack trace.

Dispatcher processing has been suspended, but messages are still being processed.

   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 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

More behaviour detected, see "new"

Community
  • 1
  • 1
João Portela
  • 6,338
  • 7
  • 38
  • 51
  • WPF has a nasty global variable that indicates that it is a state where recursion through the message loop is too dangerous to allow. A message box can certainly trigger this if it is displayed at the "wrong" time, just after WPF switched into this mode, it pumps a message loop. Your stack trace is not good enough to identify it, the exception's StackTrace is necessary. – Hans Passant Apr 24 '14 at 12:00
  • @HansPassant It's pretty much the same as the question I linked to. – João Portela Apr 24 '14 at 13:34
  • Sure, it the exact same problem. Not with the exact same cause. – Hans Passant Apr 24 '14 at 13:37
  • Could you elaborate on "When I have no control on whether a message box is shown" and "even if the messagebox is not shown at all"? – JeffRSon Apr 24 '14 at 15:37
  • "When I have no control on whether a message box is shown" - An external library is calling `MessageBox.Show`; "even if the messagebox is not shown at all" - I would rather not have the MessageBox appear than having it break my application. – João Portela Apr 24 '14 at 18:22
  • @HansPassant "WPF has a nasty global variable that indicates that it is a state where recursion through the message loop is too dangerous to allow." Do you know which global variable is that? I need it for another piece of code. – João Portela Apr 29 '14 at 15:00

0 Answers0