I'm working on a WinForms project and at one point I have to load a XmlDocument in the background. I have a BackgroundWorker do this, but when the XmlDocument can't be found the BackgroundWorker throws a System.IO.FileNotFoundException in DoWork instead of passing it onto 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();
}
}
Am I missing something here? Shouldn't the exception automatically trigger RunWorkerCompleted so it can be handled there?
Did you check if you have the System.IO.FileNotFoundException "Break when thrown" ticked in the Exception Settings ?
It might be it, as backgroundworker DoWork catch an exception if thrown.
From Microsoft (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.