Finding Out Why Control.Invoke Throws Undocumented Exceptions

A colleague was baffled by having Control.Invoke (when called from a different thread) throw an ArgumentException at him and asked if I knew what was happening. Opening the stack trace, I found that the exception originated from Control.MarshaledInvoke:

       at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
       at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
       [ user code...]

This meant that the exception was either raised from an undocumented source or that the exception had been rethrown with the original stack trace deleted.

To debug this further, I changed Invoke to BeginInvoke. to see if it might make a difference. This raised, instead of an ArgumentException, a TargetInvocationException, which in turn contained the original exception with its original stack trace. The problem was found and fixed.

No Comments