title: IDisposable and Asynchronous Processing (SmtpClient.SendAsync)
body: <p>Reviewing the MSDN docs for <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient" rel="nofollow">SmtpClient</a>, I noticed that the example code looks like this:</p>
  
  <pre><code>client.SendAsync(message, userState);
  // More stuff
  message.Dispose();
  </code></pre>
  
  <p>The call to SendAsync could take significant time to complete.  How is it that <em>message</em> remains in a useful state if it is disposed on the line following the <em>SendAsync</em> call?</p>
  
  <p>Further, <em>SmtpClient</em> implements <em>IDisposable</em>.  The MSDN example should more property be formed like</p>
  
  <pre><code>using (SmtpClient client = new SmtpClient())
  {
      // Initialize client and create the message
      using (MailMessage message = new MailMessage(from, to))
      {
          client.SendAsync(message, userState);
          // More stuff
      }
  }
  </code></pre>
  
  <p>So not only would <em>message.Dispose()</em> be called while <em>client.SendAsync()</em> is potentially still executing, but <em>client.Dispose()</em> would be called as well.</p>
  
  <p>How is it that <em>client</em> remains in a useful state for the completion of <em>SendAsync()</em> if <em>Dispose()</em> is called almost immediately after the call to <em>SendAsync()</em>?</p>
  
  <p>Or, is the MSDN example just wrong?</p>