1

I have a WPF app that has been running for 10+ years. We recently started getting the following exception on a few deployments on some machines at startup. The main app window would load and it could load some data but would then get swamped with many errors.

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)

that's it... that's the stack trace...

First of all, I understand basically what this means and that the resolution is to use Application.Current.Dispatcher.BeginInvoke() to queue the action. In fact MVVMLight has a helper function DispatcherHelper.CheckBeginInvokeOnUI to check first if the dispatcher is even needed.

However the problem is that this stack trace doesn't show me any user code, meaning I have to consider all the code I wrote in the past ten years and put MessageBox.Show('Hi 244') all over my code. And if there is a coding bug I got away with it for years and never on my machine.

The issue generally happens around startup, and if the main window does end up opening then the program seems to work as normal.

What is the application:

  • .NET Framework 4.8 / WPF / Windows 10 & 11
  • Custom shipping software used in a warehouse / customer service.
  • WPF / Xceed Data Grid / SmithHtmlEditor / MVVMLight
  • Uses Microsoft.Mshtml.DLL for HTML
  • No significant updates in years

Where is it breaking:

  • Deployed click-once applications
  • Both Windows 10 & 11
  • .NET Framework 4.8.9166.0 (from RuntimeInformation.FrameworkDescription)
  • Other framework versions too
  • Machines with and without virus protection
  • Mostly soon after startup with at least ten errors
  • Not on any of my machines ;-)

Error handling / debugging steps:

  • I have a handler on Application_DispatcherUnhandledException and TaskScheduler_UnobservedTaskException which is where I track the error
  • Scattered my code with extra Application.Current.Dispatcher.VerifyAccess() but they always passed
  • When Googling for this error - everyone else got a useful stack trace!

I suspect that a recent Windows update has changed something, but since the only failing machines are non dev machines and remote to me it's tricky (slow) to diagnose.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • 1
    Is `InnerException` null? – Lukasz Szczygielek Jun 17 '23 at 01:49
  • I meant to check that but I was pretty sure ToString() should’ve included it. I can’t connect to the warehouse again until tomorrow when someone has to approve my connection. I’ll check. – Simon_Weaver Jun 17 '23 at 02:47
  • https://github.com/dotnet/wpf/issues/3430 – Andy Jun 17 '23 at 11:29
  • @Andy thanks. I did come across that issue but I couldn’t actually see what the issue was despite them saying a new version fixed it. – Simon_Weaver Jun 17 '23 at 16:34
  • I guess your issue was due to a subtle incompatibility with a component or driver. Maybe a 32 bit one. – Andy Jun 17 '23 at 17:27
  • @Andy it could be something like that, but age between the machines is 5-10 years so it's not just unique to one certain type of workstation. Any new drivers being delivered to some of them are probably a part of a windows update, since those I can't imagine the manufacturer is still updating them. – Simon_Weaver Jun 18 '23 at 03:21

2 Answers2

1

So:

  • I managed to connect remotely into one of the Windows 11 machines
  • I installed Visual Studio 2022
  • I could duplicate the error with Ctrl + F5 in Release mode, with the same exception (and useless stack trace).
  • I then switched to Debug mode, and no error!
  • I compared all the settings for Debug and Release and tried changing Prefer 32-bit to ON (to match Debug).

enter image description here

Suddenly everything worked, and my boss is now ecstatic and saying it's never ran faster on his machine.

I remain completely stumped as to what happened and I'd really love to understand better if anyone knows, or can suggest additional diagnostics or logging I could do. I'd report this to Microsoft, but I have no repro.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • If anyone finds this helpful, please leave a short comment - especially if you're able to identify something in your code that is triggering the issue. – Simon_Weaver Jun 21 '23 at 19:31
0

Another possible lead if you can't easily recompile.

Try changing your default printer in Windows.

We have this exact issue. We traced into the .NET runtime and eventually guessed that it had to do with Printing based on the call stack. We noticed the user had a bad printer driver. When we changed the default printer to Microsoft PDF (for example) the crash went away. Sounds like a Microsoft bug me to me. At least there may be a workaround. We haven't tried this on any other systems since we just discovered this 10 minutes ago :).

From https://learn.microsoft.com/en-us/answers/questions/1181387/system-invalidoperationexception-cannot-perform-th

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689