我正在处理WinForms项目,有一次我必须在后台加载XmlDocument。我有BackgroundWorker这样做,但是当找不到XmlDocument时,BackgroundWorker在DoWork中抛出System.IO.FileNotFoundException而不是将其传递给RunWorkerCompleted。
private void LoadBgWorker_DoWork(object sender, DoWorkEventArgs e)
{
//---download manifest---
SetStatusText("Downloading manifest...");
Manifest = new System.Xml.XmlDocument();
Manifest.Load(Properties.Resources.ManifestUrl); // <-- this is where the code gets stuck, it alerts me that the exception is unhandled
}
private void LoadBgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
Success = false;
Error = e.Error;
this.Close();
}
else
{
//---loading complete, close form---
Success = true;
this.Close();
}
}
我在这里想念什么吗?异常是否不应该自动触发RunWorkerCompleted以便可以在那里处理?
您是否检查了异常设置中是否勾选了System.IO.FileNotFoundException“抛出时中断”?
可能就是这样,因为后台工作人员DoWork如果抛出该异常会捕获异常。
从Microsoft(full article here):
告诉调试器在引发异常时中断
调试器可能会在异常发生时中断执行抛出,因此您可以在调用处理程序之前检查异常。
在“例外设置”窗口中(“调试”>“ Windows”>“例外”设置),将节点展开为一类异常,例如公共语言运行时异常。然后选中该类别中的特定例外,例如System.AccessViolationException。您还可以选择整个例外类别。