Question

Im getting TargetInvocationExceptions like

An unhandled exception of type System.Reflection.TargetInvocationException occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an invocation.

because I'm calling code with

Dispatcher.Invoke(MyRoutine...)

How can I make the VS 2010 Debugger step into MyRoutine to the place where the Exception actually happens???

I know that I can build a try...catch around the Dispatcher.Invoke and then look at the inner exception, but this is quite laborious. I want the Debugger to stop where the bug happens.

Any ideas how to do that?

Was it helpful?

Solution

Dispatcher.Invoke() will setup a catch block so that it can marshal any exception back to the thread that called it. So that the caller knows that the Invoke() return value is not available. This catch block prevents the debugger from automatically breaking when the exception is thrown. The original exception is delivered as the InnerException of the TargetInvocationException object. Which might be good enough to diagnose the reason.

Do consider using BeginInvoke() instead, it doesn't need to do this kind of marshaling since the code that called it is no longer active. It is also a lot less prone to deadlock.

But if you really need the return value then only Invoke() can supply it. The way to debug an exception then is to use Debug + Exceptions and tick the Thrown checkbox for CLR exceptions. Which forces the debugger to stop when the exception is thrown.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top