Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>From what I understand, the await keyword will suspend the code flow until the function returns</p> </blockquote> <p>Well, Yes and No.</p> <ul> <li>Yes, because code flow does stop in a sense.</li> <li>No, because the thread executing this code flow does not block. (The synchronous call <code>client.GetString()</code> will block the thread).</li> </ul> <p>In fact, it will return to its calling method. To understand what it means by return to its calling method, you can read about another C# compiler magic - the <code>yield return</code> statement.</p> <p>Iterator blocks with <code>yield return</code> will break the method into a state machine - where code after the <code>yield return</code> statement will execute only after <code>MoveNext()</code> is called on the enumerator. (See <a href="http://startbigthinksmall.wordpress.com/2008/06/09/behind-the-scenes-of-the-c-yield-keyword/" rel="noreferrer">this</a> and <a href="http://csharpindepth.com/articles/chapter6/iteratorblockimplementation.aspx" rel="noreferrer">this</a>).</p> <p>Now, <code>async/await</code> mechanism is also based on similar state machine (however, its much more complicated than the <code>yield return</code> state machine).</p> <p>To simplify matters, lets consider a simple async method:</p> <pre><code>public async Task MyMethodAsync() { // code block 1 - code before await // await stateement var r = await SomeAwaitableMethodAsync(); // code block 2 - code after await } </code></pre> <ul> <li>When you mark a method with <code>async</code> identifier you tell the compiler to break the method into a state machine and that you are going to <code>await</code> inside this method.</li> <li>Lets say code is running on a thread <code>Thread1</code> and your code calls this <code>MyMethodAsync()</code>. Then <code>code block 1</code> will synchronously run on the same thread. </li> <li><code>SomeAwaitableMethodAsync()</code> will also be called synchronously - but lets say that method starts a new asynchronous operation and returns a <code>Task</code>. </li> <li>This is when <code>await</code> comes into picture. It will return the code flow back to its caller and the thread <code>Thread1</code> is free to run callers code. What happens then in calling method depends on whether calling method <code>await</code>s on <code>MyMethodAsync()</code> or does something else - but important thing is <code>Thread1</code> is not blocked.</li> <li>Now rest of await's magic - When the Task returned by <code>SomeAwaitableMethodAsync()</code> eventually completes, the <code>code block 2</code> is <em>Scheduled</em> to run.</li> <li><code>async/await</code> is built on the Task parallel library - so, this <em>Scheduling</em> is done over TPL.</li> <li>Now the thing is that this <code>code block 2</code> may not be scheduled over the same thread <code>Thread1</code> unless it had an active <code>SynchronizationContext</code> with thread affinity (like WPF/WinForms UI thread). <code>await</code> is <code>SynchronizationContext</code> aware, so, <code>code block 2</code> is scheduled over same <code>SynchronizationContext</code>, if any, when the <code>MyMethodAsync()</code> was called. If there was no active <code>SynchronizationContext</code>, then with all possibility, <code>code block 2</code> will run over some different thread.</li> </ul> <p>Lastly, I will say that since <code>async/await</code> is based on state machine created by compiler, like <code>yield return</code>, it shares some of the shortcomings - for example, you cannot <code>await</code> inside a <code>finally</code> block.</p> <p>I hope this clears your doubts.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload