title: How to dispose properly using async and await
body: <p>I'm trying to make code replacement from <code>Thread</code> to <code>Task</code>. The sleep / delay is just representing long running activity.</p>
  
  <pre><code>static void Main(string[] args)
  {
      ThreadDoWork();
      TaskDoWork();
  }
  public static void ThreadDoWork()
  {
      using (var dispose = new ThreadDispose())
      {
          dispose.RunAsync();
      }
  }
  public static async void TaskDoWork()
  {
      using (var dispose = new TaskDispose())
      {
          await dispose.RunAsync();
      }
  }
  public class ThreadDispose : IDisposable
  {
      public void RunAsync ()
      {
          ThreadPool.QueueUserWorkItem(state =&gt;
          {
              Thread.Sleep(3000);
          });
      }
      void IDisposable.Dispose()
      {
          File.AppendAllText("D:\\test.txt", "thread disposing");
      }
  }
  public class TaskDispose : IDisposable
  {
      public async Task RunAsync()
      {
          await Task.Delay(3000);
      }
      void IDisposable.Dispose()
      {
          File.AppendAllText("D:\\test.txt", "task disposing");
      }
  }
  </code></pre>
  
  <p>The result after 3 seconds in <code>test.txt</code> is only</p>
  
  <blockquote>
    <p>thread disposing</p>
  </blockquote>
  
  <p>What do I need to change in order <code>TaskDispose::Dispose</code> is always executed just like <code>ThreadDispose</code>?</p>