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)
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.