title: async - stay on the current thread?
body: <p>I've read Eric lippert's <a href="http://blogs.msdn.com/b/ericlippert/archive/2010/10/29/asynchronous-programming-in-c-5-0-part-two-whence-await.aspx" rel="noreferrer"><strong>article</strong></a> about <code>async</code> , and about confusions people had with <code>async</code> keyword. he said : </p>
  
  <blockquote>
    <p>it (<code>async</code>) means “this method contains control flow that involves awaiting
    asynchronous operations and will therefore be rewritten by the
    compiler into continuation passing style to ensure that the
    asynchronous operations can resume this method at the right spot.” <strong>The whole point of async methods it that you stay on the current thread as much as possible</strong></p>
  </blockquote>
  
  <p>I don't understand this. If I execute an asynchronous method (<code>Task</code>) and it runs , it <em>surely</em> runs on another thread.</p>
  
  <p>Moreover ,  If I write a method uses <code>await</code> , (imho) it releases the normal control flow , and code is <em>refactored</em>  alike  "<code>ContinueWith</code>" later  , on <strong>another</strong> thread.</p>
  
  <p>I tested it with (console) : </p>
  
  <pre><code>/*1*/   public void StartChain()
  /*2*/   {
  /*3*/           var a = FuncA();
  /*4*/           Console.WriteLine(a.Result);
  /*5*/   }
  /*6*/   
  /*7*/   public async Task &lt; int &gt; FuncA()
  /*8*/   {
  /*9*/           Console.WriteLine("A--" + Thread.CurrentThread.ManagedThreadId);
  /*10*/           var t = await FuncB();
  /*11*/           Console.WriteLine("B--" + Thread.CurrentThread.ManagedThreadId);
  /*12*/           return t;
  /*13*/   }
  /*14*/   
  /*15*/   public async Task &lt; int &gt; FuncB()
  /*16*/   {
  /*17*/           Console.WriteLine("C--" + Thread.CurrentThread.ManagedThreadId);
  /*18*/           await Task.Delay(2000);
  /*19*/           Console.WriteLine("D--" + Thread.CurrentThread.ManagedThreadId);
  /*20*/           return 999;
  /*21*/   }
  /*22*/   
  /*23*/   void Main()
  /*24*/   {
  /*25*/           StartChain();
  /*26*/   }
  /*27*/   
  </code></pre>
  
  <p>the result is : </p>
  
  <pre><code>A--7
  C--7
  D--17         &lt;-----D  and B are on different thread
  B--17
  999
  </code></pre>
  
  <p>So what did Eric  mean by saying  "stay on the current thread"? </p>
  
  <h1>edit 1:</h1>
  
  <p><strong>in <code>asp.net</code> it also return differnt thread ID.</strong></p>
  
  <pre><code>public async Task&lt;int&gt; FuncA()
  {
      Response.Write("&lt;br/&gt;C----" + Thread.CurrentThread.ManagedThreadId);
      var t = await FuncB();
      Response.Write("&lt;br/&gt;D----" + Thread.CurrentThread.ManagedThreadId);
      return t;
  }
  
  public async Task&lt;int&gt; FuncB()
  {
      Response.Write("&lt;br/&gt;E----" + Thread.CurrentThread.ManagedThreadId);
      await Task.Delay(2000);
      Response.Write("&lt;br/&gt;F----" + Thread.CurrentThread.ManagedThreadId);
      return 999;
  }
  
  
  
  protected async void Page_Load(object sender, EventArgs e)
  {
      Response.Write("&lt;br/&gt;A----" + Thread.CurrentThread.ManagedThreadId);
      var a=await FuncA();
      Response.Write("&lt;br/&gt;B----" + Thread.CurrentThread.ManagedThreadId);
  
  }
  
  A----8
  C----8
  E----8
  F----9
  D----9
  B----9
  </code></pre>
  
  <h1>edit  2</h1>
  
  <p><strong>(after getting an answer)</strong></p>
  
  <p><strong>it seems that thread is served only at GUI apps :. I run this code at winform</strong></p>
  
  <pre><code>  public async Task&lt;int&gt; FuncA()
          {
              textBox1.Text +=Environment.NewLine+ "\nC----" + Thread.CurrentThread.ManagedThreadId;
              var t = await FuncB();
              textBox1.Text += Environment.NewLine + "\nD----" + Thread.CurrentThread.ManagedThreadId;
              return t;
          }
  
          public async Task&lt;int&gt; FuncB()
          {
              textBox1.Text += Environment.NewLine + "\nE----" + Thread.CurrentThread.ManagedThreadId;
              await Task.Delay(2000);
              textBox1.Text += Environment.NewLine + "\nF----" + Thread.CurrentThread.ManagedThreadId;
              return 999;
          }
  
  
  
  
          private async void Form1_Load(object sender, EventArgs e)
          {
              textBox1.Text += Environment.NewLine + "\nA----" + Thread.CurrentThread.ManagedThreadId;
              var a = await FuncA();
              textBox1.Text += Environment.NewLine + "\nB----" + Thread.CurrentThread.ManagedThreadId;
          }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/iXmbq.png" alt="enter image description here"></p>