Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you're describing is not the defined behavior of BackgroundWorker. You're doing something wrong, I suspect.</p> <p>Here's a little sample that proves BackgroundWorker eats exceptions in <strong>DoWork</strong>, and makes them available to you in <strong>RunWorkerCompleted</strong>:</p> <pre><code>var worker = new BackgroundWorker(); worker.DoWork += (sender, e) =&gt; { throw new InvalidOperationException("oh shiznit!"); }; worker.RunWorkerCompleted += (sender, e) =&gt; { if(e.Error != null) { MessageBox.Show("There was an error! " + e.Error.ToString()); } }; worker.RunWorkerAsync(); </code></pre> <p>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:</p> <pre><code>var worker = new BackgroundWorker(); worker.DoWork += (sender, e) =&gt; { throw new InvalidOperationException("oh shiznit!"); }; worker.RunWorkerCompleted += (sender, e) =&gt; { // 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(); </code></pre> <p>Here's a proper implementation of the RunWorkerCompleted event handler:</p> <pre><code>private void RunWorkerCompletedHandler(object sender, RunWorkerCompletedEventArgs e) { if (e.Error == null) { DoSomethingWith(e.Result); // Access e.Result only if no error occurred. } } </code></pre> <p>VOILA, you won't receive runtime exceptions.</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