What you're describing is not the defined behavior of BackgroundWorker. You're doing something wrong, I suspect.
What you're describing is not the defined behavior of BackgroundWorker. You're doing something wrong, I suspect. Here's a little sample that proves BackgroundWorker eats exceptions in DoWork, and makes them available to you in RunWorkerCompleted: var worker = new BackgroundWorker(); worker.
DoWork += (sender, e) => { throw new InvalidOperationException("oh shiznit! "); }; worker. RunWorkerCompleted += (sender, e) => { if(e.
Error! = null) { MessageBox. Show("There was an error!" + e.Error.ToString()); } }; worker.RunWorkerAsync(); My psychic debugging skills are revealing your problem to me: You are accessing e.
Result in your RunWorkerCompleted handler -- if there's an e. Error, you must handle it without accessing e.Result. For example, the following code is bad, bad, bad, and will throw an exception at runtime: var worker = new BackgroundWorker(); worker.
DoWork += (sender, e) => { throw new InvalidOperationException("oh shiznit! "); }; worker. RunWorkerCompleted += (sender, e) => { // OH NOOOOOOOES!
Runtime exception, you can't access e. Result if there's an // error. You can check for errors using e.Error.
Var result = e. Result; }; worker.RunWorkerAsync(); Here's a proper implementation of the RunWorkerCompleted event handler: private void RunWorkerCompletedHandler(object sender, RunWorkerCompletedEventArgs e) { if (e. Error == null) { DoSomethingWith(e.
Result); // Access e. Result only if no error occurred. } } VOILA, you won't receive runtime exceptions.
1 Good point. My example pointed out the specifics of handling the error but my code would actually cause another exception if an exception was never hit in the DoWork method. – Bobby Cannon Jun 25 '09 at 15:41 Argh.
I am humbled and embarrased.... :/ I sheepishly blame my mistake on my new-dad brain and lack of sleep. Thank you, Judah. – Andy Jun 25 '09 at 16:34 Glad I could help.
Congrats on the addition to your family! – Judah mango Jun 25 '09 at 16:46 I beg to differ .... I am also struggling to see how the BGW class eats up exps ... as sometimes my app grants me access to my user area on Win7 and sometimes it doesn't. When I investigate further, I found that access being denied is because the folder doesn't exist.
I find that sometimes the error is thrown in my app and sometimes it doesn't. – IbrarMumtaz Nov 18 '09 at 14:53 @Ibrar, are you always checking for e. Error inside your RunWorkerCompleted event handler?
Make sure you check that before you do anything. All catchable exceptions will be reported there. – Judah mango Nov 18 '09 at 16:44.
I would add to the MSDN text: If the operation raises an exception that your code does not handle, the BackgroundWorker catches the exception and passes it into the RunWorkerCompleted event handler, where it is exposed as the Error property of System.ComponentModel..::. RunWorkerCompletedEventArgs. If you are running under the Visual Studio debugger, the debugger will break at the point in the DoWork event handler where the unhandled exception was raised.... AND the debugger will report the exception as "~Exception was unhandled by user code" Solution: Don't run under the debugger and it works as expected: Exception caught in e.Error.
Edit Judah has a great point. My example pointed out the specifics of handling the error but my code would actually cause another exception if an exception was never hit in the DoWork method. This example is OK due to the fact that we are specifically showing the error handling capabilities of the BackgroundWorker.
However if you are not checking the error parameter against null then this could be your issue. /Edit I don't see the same results. Can you post a little code?
Here is my code. Private void Form1_Load(object sender, EventArgs e) { BackgroundWorker worker = new BackgroundWorker(); worker. DoWork += new DoWorkEventHandler(worker_DoWork); worker.
RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); worker.RunWorkerAsync(); } void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // Will cause another exception if an exception didn't occur. // We should be checking to see if e. Error is not "null".
TextBox1. Text = "Error? " + e.
Error; } void worker_DoWork(object sender, DoWorkEventArgs e) { for (int I = 0; I Exception: BOOM at BackgroundException. Form1. Worker_DoWork(Object sender, DoWorkEventArgs e) in D:\Workspaces\Sandbox\BackgroundException\BackgroundException\Form1.Cs:line 43 at System.ComponentModel.
BackgroundWorker. OnDoWork(DoWorkEventArgs e) at System.ComponentModel. BackgroundWorker.
WorkerThreadStart(Object argument) An interesting article that looks similar to your question. It has a section on handling exceptions. http://www.developerdotstar.com/community/node/671.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.