title: .NET Web Service & BackgroundWorker threads
body: <p>I'm trying to do some async stuff in a webservice method. Let say I have the following API call: <a href="http://www.example.com/api.asmx" rel="nofollow noreferrer">http://www.example.com/api.asmx</a></p>
  
  <p>and the method is called <em>GetProducts()</em>.</p>
  
  <p>I this GetProducts methods, I do some stuff (eg. get data from database) then just before i return the result, I want to do some async stuff (eg. send me an email).</p>
  
  <p>So this is what I did.</p>
  
  <pre><code>[WebMethod(Description = "Bal blah blah.")]
  public IList&lt;Product&gt; GetProducts()
  {
      // Blah blah blah ..
      // Get data from DB .. hi DB!
      // var myData = .......
      // Moar clbuttic blahs :)  (yes, google for clbuttic if you don't know what that is)
  
      // Ok .. now send me an email for no particular reason, but to prove that async stuff works.
      var myObject = new MyObject();
      myObject.SendDataAsync();
  
      // Ok, now return the result.
      return myData;
      }
  }
  
  public class TrackingCode
  {
      public void SendDataAsync()
      {
          var backgroundWorker = new BackgroundWorker();
          backgroundWorker.DoWork += BackgroundWorker_DoWork;
          backgroundWorker.RunWorkerAsync();
          //System.Threading.Thread.Sleep(1000 * 20);
      }
  
      private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
      {
          SendEmail();
      }
  }
  </code></pre>
  
  <p>Now, when I run this code the email is never sent. If I uncomment out the Thread.Sleep .. then the email is sent.</p>
  
  <p>So ... why is it that the background worker thread is torn down? is it dependant on the parent thread? Is this the wrong way I should be doing background or forked threading, in asp.net web apps?</p>