title: async await wrong order of execution
body: <p>I am trying to get a grip of the async await pattern. Following <a href="https://msdn.microsoft.com/en-us/library/mt674882.aspx" rel="nofollow noreferrer">this</a> example, I implemented
  this small program:</p>
  <pre><code>private async void asynchWork()
  {
      // Line 1
      Task&lt;string&gt; readTask = asynchAwaitWork();
  
      // Line 2
      synchWork();
  
      // Line 3
      string result = await readTask;
  
      // Line 4
      Debug.WriteLine(result);
      Debug.WriteLine(&quot;The End&quot;);
  }
  private void synchWork()
  {
      for (int i = 0; i &lt; 5; i++)
      {
          Debug.WriteLine(&quot;SynchWork For i : &quot; + i);
          Thread.Sleep(750);
      }
  }
  
  private async Task&lt;string&gt; asynchAwaitWork()
  {
      for (int i = 0; i &lt; 15; i++)
      {
          Debug.WriteLine(&quot;A-SynchWork For i : &quot; + (i+111));
          Thread.Sleep(750);
      }
  
      return &quot;finished&quot;;
  }
  </code></pre>
  <p>I expected that Line 1 is executed, then Line 2 and Line 4 will have to wait until Line 3 is finished and returns a string.
  But looking at the output the <code>synchWork</code> method is executed after the <code>asynchWork</code> method.</p>
  <blockquote>
  <p>A-SynchWork For i : 111</p>
  <p>A-SynchWork For i : 112</p>
  <p>A-SynchWork For i : 113</p>
  <p>A-SynchWork For i : 114</p>
  <p>A-SynchWork For i : 115</p>
  <p>...</p>
  <p>A-SynchWork For i : 125</p>
  <p>SynchWork For i : 0</p>
  <p>SynchWork For i : 1</p>
  <p>SynchWork For i : 2</p>
  <p>SynchWork For i : 3</p>
  <p>SynchWork For i : 4</p>
  <p>finished</p>
  <p>The End</p>
  </blockquote>
  <p>I expected the <code>synchWork</code> method to be executed in the time when the <code>asynchWork</code> method is buisy and waits in Line 3 for the result.
  What did I misunderstood in the example? Or is it just that the output of <code>Debug.WriteLine</code> is executed in the end sequentially but the real order of working is according to my expectations?</p>
  <p>EDIT:
  With the help of the comments and answers I learned:</p>
  <ul>
  <li><p>Don't ignore the warning and research their meaning ;)</p>
  </li>
  <li><p>using the <code>await</code> keyword allows to wait for the result of an operation which is executed by a created <code>Task</code> inside the method.</p>
  </li>
  </ul>
  <p>EDIT 2:</p>
  <ul>
  <li>But the Task has to be started! otherwise there will be no parallel processing and the compiler will chase it through the main thread and Line 3 has actually only a blocking function for Line 4 not more.</li>
  </ul>
  <p>since <code>Thread.Sleep(750)</code> is only a simulated work and I don't really need <code>Delay</code> this also works:</p>
  <pre><code>private async Task&lt;string&gt; asynchAwaitWork()
  {
      await Task.Run(()=&gt; {
          for (int i = 0; i &lt; 15; i++)
              {
                  Debug.WriteLine(&quot;A-SynchWork For i : &quot; + (i+111));
                  Thread.Sleep(750);
              }
              });
      return &quot;finished&quot;;            
  }
  </code></pre>
  <p>Thank you very much. I learned a lot :)</p>