BackgroundWorker 没有将 FileNotFoundException 传递给 RunWorkerCompleted
BackgroundWorker not passing FileNotFoundException to RunWorkerCompleted
我正在处理一个 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 "Break when thrown"?
可能是这样,因为 backgroundworker DoWork 会在抛出时捕获异常。
来自微软(full article here):
Tell the debugger to break when an exception is thrown
The debugger can break execution at the point where an exception is
thrown, so you may examine the exception before a handler is invoked.
In the Exception Settings window (Debug > Windows > Exception
Settings), expand the node for a category of exceptions, such as
Common Language Runtime Exceptions. Then select the check box for a
specific exception within that category, such as
System.AccessViolationException. You can also select an entire
category of exceptions.
我正在处理一个 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 "Break when thrown"?
可能是这样,因为 backgroundworker DoWork 会在抛出时捕获异常。
来自微软(full article here):
Tell the debugger to break when an exception is thrown
The debugger can break execution at the point where an exception is thrown, so you may examine the exception before a handler is invoked.
In the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. Then select the check box for a specific exception within that category, such as System.AccessViolationException. You can also select an entire category of exceptions.