Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The most important thing to know about <code>async</code> and <code>await</code> is that <code>await</code> <em>doesn't</em> wait for the associated call to complete. What <code>await</code> does is to return the result of the operation immediately and synchronously <em>if the operation has already completed</em> or, if it hasn't, to schedule a continuation to execute the remainder of the <code>async</code> method and then to return control to the caller. When the asynchronous operation completes, the scheduled completion will then execute.</p> <p>The answer to the specific question in your question's title is to block on an <code>async</code> method's return value (which should be of type <code>Task</code> or <code>Task&lt;T&gt;</code>) by calling an appropriate <code>Wait</code> method:</p> <pre><code>public static async Task&lt;Foo&gt; GetFooAsync() { // Start asynchronous operation(s) and return associated task. ... } public static Foo CallGetFooAsyncAndWaitOnResult() { var task = GetFooAsync(); task.Wait(); // Blocks current thread until GetFooAsync task completes // For pedagogical use only: in general, don't do this! var result = task.Result; return result; } </code></pre> <p>In this code snippet, <code>CallGetFooAsyncAndWaitOnResult</code> is a <em>synchronous</em> wrapper around asynchronous method <code>GetFooAsync</code>. However, this pattern is to be avoided for the most part since it will block a whole thread pool thread for the duration of the asynchronous operation. This an inefficient use of the various asynchronous mechanisms exposed by APIs that go to great efforts to provide them.</p> <p>The answer at <a href="https://stackoverflow.com/questions/12235967/await-doesnt-wait-for-the-completion-of-call">&quot;await&quot; doesn&#39;t wait for the completion of call</a> has several, more detailed, explanations of these keywords.</p> <p>Meanwhile, @Stephen Cleary's guidance about <code>async void</code> holds. Other nice explanations for why can be found at <a href="http://www.tonicodes.net/blog/why-you-should-almost-never-write-void-asynchronous-methods/" rel="noreferrer">http://www.tonicodes.net/blog/why-you-should-almost-never-write-void-asynchronous-methods/</a> and <a href="http://www.jaylee.org/post/2012/07/08/c-sharp-async-tips-and-tricks-part-2-async-void.aspx" rel="noreferrer">http://www.jaylee.org/post/2012/07/08/c-sharp-async-tips-and-tricks-part-2-async-void.aspx</a>.</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.
    3. 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