title: C# async/await strange behavior in console app
body: <p>I built some async/await demo console app and get strange result. Code:</p>
  
  <pre><code>class Program
  {
      public static void BeginLongIO(Action act)
      {
          Console.WriteLine("In BeginLongIO start... {0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
          Thread.Sleep(1000);
          act();
          Console.WriteLine("In BeginLongIO end... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
      }
  
      public static Int32 EndLongIO()
      {
          Console.WriteLine("In EndLongIO start... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
          Thread.Sleep(500);
          Console.WriteLine("In EndLongIO end... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
          return 42;
      }
  
      public static Task&lt;Int32&gt; LongIOAsync()
      {
          Console.WriteLine("In LongIOAsync start... {0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
          var tcs = new TaskCompletionSource&lt;Int32&gt;();
          BeginLongIO(() =&gt;
          {
              try { tcs.TrySetResult(EndLongIO()); }
              catch (Exception exc) { tcs.TrySetException(exc); }
          });
          Console.WriteLine("In LongIOAsync end... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
          return tcs.Task;
      }
  
      public async static Task&lt;Int32&gt; DoAsync()
      {
          Console.WriteLine("In DoAsync start... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
          var res = await LongIOAsync();
          Thread.Sleep(1000);
          Console.WriteLine("In DoAsync end... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
          return res;
      }
  
      static void Main(String[] args)
      {
          ticks = DateTime.Now.Ticks;
          Console.WriteLine("In Main start... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
          DoAsync();
          Console.WriteLine("In Main exec... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
          Thread.Sleep(3000);
          Console.WriteLine("In Main end... \t\t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId);
      }
  
      private static Int64 ticks;
  }
  </code></pre>
  
  <p>The result bellow:</p>
  
  <p><a href="https://i.stack.imgur.com/rQ1Y1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rQ1Y1.png" alt="enter image description here"></a></p>
  
  <p>Maybe I do not fully understand what exactly makes await. I thought if the execution comes to await then the execution returns to the caller method and task for awaiting runs in another thread. In my example all operations execute in one thread and the execution doesn't returns to the caller method after await keyword.
  Where is the truth?</p>