title: C# "async and await" feature and threads
body: <p>Please see the following codes. </p>
  
  <pre><code>            class AddParams
              {
                  public int a, b;
  
                  public AddParams(int numb1, int numb2)
                  {
                      a = numb1;
                      b = numb2;
                  }
              }
  
              class Program
              {
                  static void Main(string[] args)
  
                  {
                      Console.WriteLine("ID of thread in 1: {0}",
                        Thread.CurrentThread.ManagedThreadId);
  
                      AddAsync();
                      Console.ReadLine();
                  }
  
                  private static async Task AddAsync()
                  {
                      Console.WriteLine("***** Adding with Thread objects *****");
                      Console.WriteLine("ID of thread in Main(): {0}",
                        Thread.CurrentThread.ManagedThreadId);
  
                      AddParams ap = new AddParams(10, 10);
                      await Sum(ap);
  
                      Console.WriteLine("Other thread is done!");
                  }
  
                  static async Task Sum(object data)
                  {
                      await Task.Run(() =&gt;
                      {
                          if (data is AddParams)
                          {
                              Console.WriteLine("ID of thread in Add(): {0}",
                                Thread.CurrentThread.ManagedThreadId);
  
                              AddParams ap = (AddParams)data;
                              Console.WriteLine("{0} + {1} is {2}",
                                ap.a, ap.b, ap.a + ap.b);
                          }
                      });
                  }
              }
  </code></pre>
  
  <p>And the result is this:</p>
  
  <pre><code>***** Adding with Thread objects *****
      ID of thread in Main(): 1
      ID of thread in Add(): 3
      10 + 10 is 20
      Other thread is done!
  </code></pre>
  
  <p>I get the result that is unlike what I expected.
  Please correct my assumptions with giving right concepts.</p>
  
  <p>1.
  I assume that Main() is invoked on the primary thread, say Thread1.</p>
  
  <p>2.
  Invoke AddAsync() but this method is marked with async, so this method is possibly invoked on a secondary thread, say Thread2.
  But the result of the following code inside of AddAsync() method says Thread1 unlike Thread2 which I expected:</p>
  
  <pre><code>Console.WriteLine("ID of thread in Main(): {0}",Thread.CurrentThread.ManagedThreadId);
  </code></pre>
  
  <p>3.
  Invoke Sum() method. but by decoration by await keyword, Sum() method is invoked on a secondary thread, say Thread3.  </p>
  
  <ol start="4">
  <li>Inside of Sum() method, Run() method is invoked on the secondary thread, say Thread4.</li>
  </ol>
  
  <p>I'm understanding at this moment like this:
  When I do asynchronous programming, creating new threads depend on the CLR. CLR creates multi-threads as it could as possible, but CLR also can process asynchronous tasks on a single thread, just by processing multi tasks at the same time asynchronously on a single thread.
  I just assume simple case that every time I attempt asynchronous tasks, CLR creates a new thread.</p>
  
  <p>I know this topic is difficult to depict so it would be nicer if it would be explained with drawings.</p>
  
  <p>Additional questions.  </p>
  
  <p>1.
  The following code inside of AddAsync() method really indicates ID of thread in "Main()"? For me, it would be more appropriate with "ID of thread in AddAsync()".</p>
  
  <pre><code>Console.WriteLine("ID of thread in Main(): {0}", Thread.CurrentThread.ManagedThreadId);
  </code></pre>