After a lot of searching:
The exception is caused by external code, namely the loading animation that appears when the reportviewer is rendering the report.
That external code is something that I cannot change..
Communicating with Microsoft support proved to be a dead end.
The only thing in my power is to find a way to prevent the exception from terminating the application.
You'll find some answers on the web that suggests adding the following line to the app.config file:
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="1" />
</runtime>
</configuration>
but that doesn't work with .Net 5
====================================
The solution for me was:
in the Application.xaml.vb file, I added the following code:
Private Sub Application_Startup(sender As Object, e As StartupEventArgs)
SetupExceptionHandling()
End Sub
Private Sub SetupExceptionHandling()
AddHandler AppDomain.CurrentDomain.UnhandledException, Sub(s, e)
Dim x = CType(e.ExceptionObject, Exception)
If x.GetType() = GetType(OutOfMemoryException) Then
LogError("Out Of Memory Exception")
Application.Current.Shutdown()
Else
LogException(CType(e.ExceptionObject, Exception))
Thread.CurrentThread.Join()
End If
End Sub
AddHandler DispatcherUnhandledException, Sub(s, e)
LogException(e.Exception)
e.Handled = True
End Sub
AddHandler TaskScheduler.UnobservedTaskException, Sub(s, e)
LogException(e.Exception)
e.SetObserved()
End Sub
End Sub
Private Sub LogException(ex As Exception)
End Sub
The key points here are:
-handling the AppDomain.CurrentDomain.UnhandledException as the exception is not caught by the DispatcherUnhandledException
-Thread.CurrentThread.Join() this is the line that prevents the app from terminating.
-some could argue that this is a bad idea since it allows the app to continue in a potentially incorrect state..
which is a valid point, alas I couldn't find a better solution to prevent code that I have no control over from terminating the application, and thus I excluded OutOfMemoryException from the stop termination process..
and might exclude other types of exceptions or add some logic to guard against faulty state..