Backgroundworker handles exception wrongly

D

ditnooitlezen

Hi,

the (.NET 2.0) backgroundworker object has a DoWork method that
operates in a background thread. When the DoWork method is finished the
RunWorkerCompleted event is raised in the parent thread.

Now I have in the backgroundworker's DoWork method a try catch finally
block, like this:

StreamReader sr = null;
try
{
sr = new StreamReader("blahdieblah" +
e.Argument.ToString());
if (!this.CancellationPending)
try
{
e.Result = sr.ReadToEnd();
}
catch (Exception exc)
{
throw exc;
}
else
{
e.Cancel = true;
e.Result = null;
}
}
catch (Exception exc)
{
throw exc;
}
finally
{
if (sr != null) sr.Close();
}
return;

The "blahdieblah" part is to force an error. The 'e' is een event
argument that is passed to the DoWork method.
The error is catched, but code execution always breaks at the 'throw
exc' line.
I presumed that the error should have been propagated to the
RunWorkerCompletedEventArgs, in it's .Error property. However it never
gets there.

What did I miss? When and how will the .Error property be filled and
RunWorkerCompleted called, like the documentation states.
 
D

ditnooitlezen

I hereby add the following new information to the question:
- it appears that by removing the 'catch' block, the exceptionis
raised to the parent thread (shouldn't 'throw exc' have done the
same?)
- the application then generates a 'first chance exception' (green bar
break - only when in debug mode)

Question 2: is it perhaps a better practice to propagate the exception
"manually" in the Result property of the RunWorkerCompleted event?

Thanks for lookin in to this.



(e-mail address removed) schreef:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top