Nick Sologoub
Nick Sologoub

Reputation: 734

InvalidOperationException in a new WPF window when I interact with a control

I have a word add-in that adds custom toolbar to UI. There is a button on that toolbar that upon clicking opens a WPF window.

I am getting some weird behaviour around that window recently. When the window opens as soon as I try to interact with any controls on it (i.e. clicking into textbox to start typing, checking checkbox etc). I get numerous InvalidOperationException:

************** Exception Text **************

System.InvalidOperationException: 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 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

This only happens if I interact with controls in the window. It is not related to any specific control, I've removed everything and just created and empty textbox in the middle of the window. Still, as soon as I click into it, the app crashes.

Now I know that this exception basically tells me that I might be performing an action that modifies visual tree in the middle of visual tree modification (like showing message box on visibility changed). But I am not doing anything like that. I am just opening a window and then click on a control inside.

Anyone have any ideas on the cause?

Upvotes: 0

Views: 483

Answers (1)

Nick Sologoub
Nick Sologoub

Reputation: 734

For anyone interested, I was managed to fix the problem eventually. It turned out that the wpf dialogue was actually a WindowsForms form with elementhost in it. There was some code in the parent WinForms form that was executed on VisibleChanged event of an ElementHost:

this.TopMost = true;
Application.DoEvents();
this.TopMost = false;

This was to ensure that the dialogue is always shown on top when it was first shown. As soon as this piece of code was removed, the exception disappeared.

Upvotes: 1

Related Questions