4

I've started getting an InvalidOperationException in VS2012 Ultimate saying:

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

...whenever I call a MessagBox anywhere in a UserControl's code-behind. This currently appears to affect every project on my computer, including those that I've downloaded and a fresh project with no real code other than the UserControl itself. This only seems to affect UserControls, however; standard classes and Windows' code-behinds work fine.


System.InvalidOperationException occurred

HResult=-2146233079

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

Source=WindowsBase

StackTrace: at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

InnerException:


InnerException is blank because according to VS, it's null. Does this issue make any sense to anybody? After a Google search, I found a few instances where this exception had been thrown, but none of those situations were anything like this. I'm hoping somebody here might have some knowledge on what may be causing this and could possibly be able to help me before I resort to reinstalling VS and hoping for the best.

Any ideas at all on how to fix this would be greatly appreciated.


Per Mark Hall's request: A class as simple as this will cause the exception:

public partial class TestView : UserControl
{
    public TestView()
    {
        InitializeComponent();

        System.Windows.MessageBox.Show("Test");
    }
}
Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101
Jason D
  • 2,634
  • 6
  • 33
  • 67
  • Please show the code you are using to instantiate the MessageBox – Mark Hall Sep 21 '13 at 21:36
  • Seems like you can show the MessageBox on the UI thread to avoid the exception http://stackoverflow.com/questions/7442943/dispatcher-throws-invalidoperationexception-on-messagebox-show-in-textchanged-ev – keyboardP Sep 21 '13 at 21:49
  • 6
    It is a very ugly WPF problem, the dispatcher loop allows *recursion* and there are lots of places inside the WPF plumbing where it cannot tolerate that. So it arbitrarily set a flag "don't recurse because hell will freeze over if you do". A good way to recurse anyway is to use MessageBox.Show(), a method that *has* to recurse to keep the message box window alive. The only cure is to not call it in inappropriate places. Definitely not in a control's constructor. MessageBox is no substitute for a debugger breakpoint. – Hans Passant Sep 21 '13 at 21:51
  • Thank you for the replies. I'm just glad it's a WPF issue and not a computer issue. – Jason D Sep 21 '13 at 22:08
  • I created a fresh user control with a message box in the constructor and my visual studio froze – jamesSampica Sep 21 '13 at 23:50
  • 2
    @HansPassant "The only cure is to not call it in inappropriate places." - is there a way to know if I'm in one of those "inappropriate places"? – João Portela Apr 24 '14 at 18:26

1 Answers1

1

Hans Passant is right, you really shouldn't try to show a message box during the constructor. The constructor is being called not only at run-time, but at design-time as well.

If you're using this for debugging purposes, try writing to a log file, or Console.WriteLine(...) instead.

Eric
  • 2,207
  • 2
  • 16
  • 16