473,547 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ThreadException and RunworkerComple tedEventArgs Error property

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

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

Application.Thr eadException += new
ThreadException EventHandler(Ap plication_Threa dException);

void Application_Thr eadException(ob ject sender,
ThreadException EventArgs e)
{
DialogResult result = MessageBox.Show (
"Custom Error Message",
"Applicatio n Exception Reported",
MessageBoxButto ns.YesNo,
MessageBoxIcon. Exclamation);

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

Here's my _RunWorkerCompl eted() code:

private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs 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?

Dec 15 '06 #1
1 4650
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
TargetInvocatio nException 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.

--
Dave Sexton

"Mesan" <93*****@gmail. comwrote in message
news:11******** *************@l 12g2000cwl.goog legroups.com...
Has anyone else noticed that even though you handle an exception that
happens within the background worker that the UnhandledExcept ion event
still fires?

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

Application.Thr eadException += new
ThreadException EventHandler(Ap plication_Threa dException);

void Application_Thr eadException(ob ject sender,
ThreadException EventArgs e)
{
DialogResult result = MessageBox.Show (
"Custom Error Message",
"Applicatio n Exception Reported",
MessageBoxButto ns.YesNo,
MessageBoxIcon. Exclamation);

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

Here's my _RunWorkerCompl eted() code:

private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs 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?

Dec 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1498
by: Giovanni pepe | last post by:
Hello, in a VB.NET Application I had a problem after a Drag&Drop operation from Windows Desktop (Dataformats.filedrop) to listview.... In debug there are no errors so I've decided to implement the application ThreadException event (throug addhandler in sub main) After this the problem was disappeared... Why? Thank You
12
2672
by: Rakesh | last post by:
Hi, I am catching unhandled exception by attached an event handler on the Application.ThreadException event. This will ensure that I could handle all uncaught exceptions on the main thread. But, suppose I create a new thread within my app and an unhandled exception is generated in that thread, will that exception be caught here?
2
1187
by: Robert Scheer | last post by:
Hi. I am using Application.ThreadException together with a sub to catch unhandled exceptions on one form. My application has 4 forms. Do I need to add an event handler for the Application.ThreadException for each one of my forms? Thanks, Robert Scheer
2
4765
by: Alex | last post by:
My Problem is that although i have added a handler to Application.ThreadException and it works fine for my application exceptions, it does not fire if an exception is generated inside an event handler of my App. eg. If i throw a exception inside DragDrop, or MouseDown of an event the ThreadException does not fire !! Any ideas ???
5
3915
by: Matthew Hood | last post by:
I have a DLL I created to handle all unknown errors using the Application.ThreadException event. I'm asking the user if they want to terminate the program or continue. This is working like I want. The program exits when I tell it to terminate, and when I continue, the program keeps running but the routine that generated the error exited. I...
2
1492
by: paul.mason | last post by:
I was wondering if anyone else had come across this "feature" of .NET and if they had any idea what might be causing it. I've been writing my first C# Windows application (so if there's anything so appalling in my code that it gives experienced developers a touch of the vapours, I hope you'll be indulgent!) I've written my own code to...
2
5385
by: Steve | last post by:
After running into a design wall with my traditional thread approach, I realized that a BackgroundWorker object would fit my needs pretty good. Never really used them before. In a nutshell, my problem is cancelling the worker. I've read the MSDN docs, checked google and haven't found a solution. The problem is, once I make a call to...
0
1511
by: pagates | last post by:
Hi All, I'm playing with the BackgroundWorker control, and am having a little trouble figuring out Exception Handling. MSDN makes it sound so simple - if an exception is thrown in DoWork, then e.Error should not be null. However, I can't get DoWork to process an exception "properly". I always get an Unhandled Exception error. For...
1
1383
by: Andrus | last post by:
T reproduce, run code. Observed: Vista dialog Myapp stops working Expected: Messagebox. If RaiseTypeLoadException() is removed from Main(), Messagebox occurs. ThreadException event does not occur before MainForm is instantiated. How to fix ?
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.