title: BackgroundWorker exception handling
body: <p>I'm working with the following components:</p>
  
  <ul>
  <li>a Library (which throws an exception)</li>
  <li>a test-console to test my logging</li>
  <li>the enterprise library exception handling application blocks</li>
  <li>the enterprise library logging application blocks</li>
  </ul>
  
  <p>I'm invoking the library method by using a backgroundworker. The library throws the exception but the RunWorkerCompleted handler is never called. </p>
  
  <p>The only way to catch the exception is to surround my DoWork handler code with a try/catch block.</p>
  
  <p>Did is misunderstand the RunWorkerCompletedEventArgs.Error Property? Isn't it for getting exceptions which got caught by the BackgroundWorker?</p>
  
  <p>Codesample:</p>
  
  <pre><code>static BackgroundWorker w = new BackgroundWorker();
  
  w.DoWork += new DoWorkEventHandler(w_DoWork);
  w.RunWorkerCompleted += 
     new RunWorkerCompletedEventHandler(w_RunWorkerCompleted);
  w.RunWorkerAsync();
  
  
  
  static void w_DoWork(object sender, DoWorkEventArgs e)
  {
     MyClass m  = new MyClass();
     w.result = m.Compute();
  }
  
  static void w_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
     if (e.Error != null)
     {
        HandleException(e.Error);
     }
  
     /* result related code */
  
  }
  
  
  static void HandleException(Exception e)
  {
     ExceptionPolicy.HandleException(e, "MyPolicy");
  }
  </code></pre>
  
  <p>The above sample leads to a termination of my console application. The vs2010 output writes absolutely nothing (only default output).</p>
  
  <p>So where's the problem?</p>
  
  <p>//Edit: this snippet works for catching the library's exception.</p>
  
  <pre><code>static void w_DoWork(object sender, DoWorkEventArgs e)
  {
     try
     {
        MyClass m  = new MyClass();
        w.result = m.Compute();
     }catch(Exception e){ }
  
  }
  </code></pre>