如何找到 BackgroundWorker 线程中抛出异常的位置?

How to find where the exception was thrown inside a BackgroundWorker thread?

好的,这应该是基本的,但我找不到好的答案。 我只想在 BackgroundWorker 内部抛出的异常处中断 VS。 代码示例:

    private void btnBGWorker_Click(object sender, EventArgs e)
    {
        BackgroundWorker bgWorker = new BackgroundWorker();
        bgWorker.DoWork += bgWorker_DoWork;
        bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
        bgWorker.RunWorkerAsync();
    }

    void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //I know e.Error have the Exception but
        if (e.Error != null)
            throw e.Error; //Well this does not work, VS will throw TargetInvocationException
    }

    void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        //** How can I make VS break here !!!??? **
        throw new NotImplementedException();
    }

您可以转到 "Debug > "Exceptions" 菜单,然后确保 "Common Language Runtime Exceptions" 旁边的 "Thrown" 列中有复选标记。这将使 Visual Studio 中断从您的 CLR 代码中抛出的所有异常。