title: Which thread does the callback run on?
body: <p>I've written a small application that calls an Asynchronous method using <code>BeginInvoke</code>.</p>
  
  <pre><code>// Asynchronous delegate
  Func&lt;int, MailItemResult&gt; method = SendMail;
  
  // Select some records from the database to populate col
  
  while (i &lt; col.Count)
  {
      method.BeginInvoke(i, SendMailCompleted, method);
      i++;
  }
  
  Console.ReadLine();
  </code></pre>
  
  <p>This is in the <code>Main()</code> method of a Console Application.  <code>MailItemResult</code> is defined as:</p>
  
  <pre><code>class MailItemResult
  {
      public int CollectionIndex { get; set; }
      public bool Success { get; set; }
      public DateTime DateUpdated { get; set; }
  }
  </code></pre>
  
  <p>Nice and simple.  And the callback method is defined as follows:</p>
  
  <pre><code>static void SendMailCompleted(IAsyncResult result)
  {
      var target = (Func&lt;int, MailItemResult&gt;)result.AsyncState;
      MailItemResult mir = target.EndInvoke(result);
      if (mir.Success)
      {
          // Update the record in the database
      }
  }
  </code></pre>
  
  <p>This application runs for the first 100ish threads and then throws up a deadlock in the database.  Now, I understand deadlocks, but what I'm failing to understand in this small application is in which thread is the callback method (<code>SendMailCompleted</code>) being called?  Is this called from the main application thread?  Or is it using the same thread, from the thread pool, that the <code>BeginInvoke</code>method was using?</p>