Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>[Edit]</p> <p>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.</p> <p>[/Edit]</p> <p>I don't see the same results. Can you post a little code? Here is my code.</p> <pre><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 &lt; 10; i++) { if (i &lt; 5) { Thread.Sleep(100); } else { throw new Exception("BOOM"); } } } </code></pre> <p>Program Output:</p> <blockquote> <p>Error? System.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)</p> </blockquote> <p>An interesting article that looks similar to your question. It has a section on handling exceptions.</p> <p><a href="http://www.developerdotstar.com/community/node/671" rel="nofollow noreferrer">http://www.developerdotstar.com/community/node/671</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload