ThreadException and RunworkerCompletedEventArgs Error property

M

Mesan

Has anyone else noticed that even though you handle an exception that
happens within the background worker that the UnhandledException event
still fires?

For example, here's my ThreadException hookup and code :

Application.ThreadException += new
ThreadExceptionEventHandler(Application_ThreadException);

void Application_ThreadException(object sender,
ThreadExceptionEventArgs e)
{
DialogResult result = MessageBox.Show(
"Custom Error Message",
"Application Exception Reported",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation);

// Exits the program when the user says no.
if (result == DialogResult.No)
Application.Exit();
}

Here's my _RunWorkerCompleted() code:

private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("I handled the error, why does the
ThreadException event still fire?");
}
}

So, here's the question -- why do I get both error messages?
 
D

Dave Sexton

Hi,

We can't reproduce the problem unless you provide a short but complete
program. (See Jon Skeet's article on "short but complete programs":
http://www.yoda.arachsys.com/csharp/complete.html)

If you're trying to access the e.Result property when e.Error != null then a
TargetInvocationException will be thrown. Its InnerException provides a
message describing that the result of the operation is invalid, and the
InnerException's InnerException is the same as e.Error, the actual exception
that was thrown.

If your not accessing the e.Result property then please verify whether the
exception being thrown in both methods are the same, or at least one of the
InnerExceptions caught by the ThreadException event handler is the same as
e.Error.
 

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