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

时间:2015-01-20 12:17:47

标签: c# exception backgroundworker

好的,这应该是基本的,但我无法找到一个好的答案。 我只想在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();
    }

1 个答案:

答案 0 :(得分:1)

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