简体   繁体   中英

How to debug *Invoke() in Visual Studio 2008

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());
    }
}

调试菜单>例外>检查“投掷”框,查看要在抛出调试器时进入调试器的任何异常。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM