title: catching exceptions from another thread
body: <p>I have a method running in a seperate thread.  The thread is created and started from a form in a windows application.  If an exception is thrown from inside the thread, what is the best way to pass it back to the main application.  Right now, I'm passing a reference to the main form into the thread, then invoking the method from the thread, and causing the method to be called by the main application thread.  Is there a best practice way to do this because I'm not comfortable with how I'm doing it now.</p>
  
  <p>Example of my form:</p>
  
  <pre><code>public class frmMyForm : System.Windows.Forms.Form
  {
      /// &lt;summary&gt;
      /// Create a thread
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;&lt;/param&gt;
      /// &lt;param name="e"&gt;&lt;/param&gt;
      private void btnTest_Click(object sender, EventArgs e)
      {
          try
          {
              //Create and start the thread
             ThreadExample pThreadExample = new ThreadExample(this);
             pThreadExample.Start();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message, Application.ProductName);
          }
      }
  
      /// &lt;summary&gt;
      /// Called from inside the thread 
      /// &lt;/summary&gt;
      /// &lt;param name="ex"&gt;&lt;/param&gt;
      public void HandleError(Exception ex)
      {
          //Invoke a method in the GUI's main thread
          this.Invoke(new ThreadExample.delThreadSafeTriggerScript(HandleError), new Object[] { ex });
      }
  
      private void __HandleError(Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
  }
  </code></pre>
  
  <p>Example of my thread class:</p>
  
  <pre><code>public class ThreadExample
  {
      public delegate void delThreadSafeHandleException(System.Exception ex);
  
      private Thread thExample_m;
  
      frmMyForm pForm_m;
      private frmMyForm Form
      {
          get
          {
              return pForm_m;
          }
      }
  
      public ThreadExample(frmMyForm pForm)
      {
          pForm_m = pForm;
  
          thExample_m = new Thread(new ThreadStart(Main));
          thExample_m.Name = "Example Thread";
      }
  
      public void Start()
      {
          thExample_m.Start();
      }
  
      private void Main()
      {
          try
          {
              throw new Exception("Test");
          }
          catch (Exception ex)
          {
              Form.HandleException(ex);
          }
      }
  }
  </code></pre>