views:

88

answers:

2

I have a problem debugging Invoke() or BeginInvoke() in Visual Studio 2008.

For instance in the code below the debugger breaks on the Invoke() call. I would have liked it to break on Console.WriteLine(p.ToString()); because that is where an exception is thrown. In code as simple as this this is not that much of a problem but it can get really annoying in more complex code. (With BeginIvoke() things even get worse because then the debugger breaks on Application.Run(new Form1());)

Is there any way to make the debugger break at the location of the original exception?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Invoke(new Action(MyMethod));
    }

    private void MyMethod()
    {
        object p = null;
        Console.WriteLine(p.ToString());
    }
}
+4  A: 

Debug menu > Exceptions > Check the "Thrown" box for any exception you want to break into the debugger at the point it's thrown.

280Z28
A: 

Something else you can do, as you venture into this area of development, is to enable the debug threading window. Whaen you hit breakpoints and step through the code, make note of the threads (you can even name them for clarification) to help you better understand what is happening when your code executes (and invokes)..

This is especially helpful when you are using background threads, timers, events, etc., and need to invoke back to the UI thread.

Doug L.