Questions tagged [async-await]

This covers the asynchronous programming model supported by various programming languages, using the async and await keywords.

Several programming languages support an asynchronous programming model using co-routines, with the async and await keywords.

Support for the model was added to

  • C# and VB in VS2012
  • Python in 3.5
  • ECMAScript in ECMAScript 2017
  • Rust in 1.39
  • C++ in C++20
  • Swift in Swift 5.5

C# and Visual Studio

Asynchronous programming with async and await was introduced with C# 5.0 in Visual Studio 2012. The run-time support for this language concept is a part of .NET 4.5 / Windows Phone 8 / Windows 8.x Store Runtime.

It's also possible to use async/await and target .NET 4.0 / Windows Phone 7.1 / Silverlight 4 / MonoTouch / MonoDroid / Portable Class Libraries, with Visual Studio 2012+ and Microsoft.Bcl.Async NuGet package, which is licensed for production code.

Async CTP add-on for VS2010 SP1 is also available, but it is not suitable for product development.

Python

Similar syntax was introduced to Python 3.5 (see PEP 492 - Coroutines with async and await syntax.

Previously, it was possible to write co-routines using generators; with the introduction of await and async co-routines were lifted to a native language feature.

C++

Coroutines is introduced in C++20. Using the co_await operator results in suspended execution until resumed. Values can be returned using co_yield and co_return keywords which correspond to suspending and completing execution, respectively.

Swift

The async-await pattern was introduced in Swift 5.5 at WWDC 2021, as part of a broader Swift concurrency initiative. Historically, asynchronous patterns were achieved through the use of Grand Central Dispatch (GCD, ) and “completion handler closure” patterns. The Swift concurrency aims to provide a more intuitive asynchronous coding environment.

ECMAScript

The async and await keywords were first reserved in the ECMAScript 2016 specification and then their use and behavior was fully-specified in the ECMAScript 2017 specification.

Historically, ECMAScript offered “promises”, an improvement over traditional callback patterns, where this sort of looping and exception handling is challenging. Task.js and similar libraries further refined promises, to further simplify the process. But with async functions, all the remaining boilerplate is removed, leaving only the semantically meaningful code in the program text.

Asynchronous vs multi-threaded

The async-await pattern simplifies the writing of asynchronous code. While it is frequently used in multi-threaded environments, it should be noted that “asynchronous” and “multi-threaded” represent two different concepts. The async and await keywords merely allow us to represent relationships and dependencies between asynchronous tasks in a more natural manner, which may be distinct from the mechanism to run code on a different thread. The async-await pattern offers great utility in a multi-threaded environment, but it is not, itself, the multi-threaded mechanism.

Resources:

C#

C++

Swift

Related:

26583 questions
11
votes
3 answers

What do I do with async Tasks I don't want to wait for?

I am writing a multi player game server and am looking at ways the new C# async/await features can help me. The core of the server is a loop which updates all the actors in the game as fast as it can: while (!shutdown) { foreach (var actor in…
11
votes
3 answers

HttpClient GetAsync always says 'WaitingForActivation'

I am new to HttpClient. My code below always says "WaitingForActivation" in the status. Please help private static async Task MakeCall() { var httpclient = new HttpClient(); var response = await…
user1615476
  • 161
  • 2
  • 2
  • 7
11
votes
2 answers

Turn a sync method into an async one

I'm trying to turn a synchronous method from some old code into an asynchronous method, but I'm having some trouble understand. From all the videos and tutorials I've read they seem to be creating two methods: one the actual function, the other a…
James Jeffery
  • 12,093
  • 19
  • 74
  • 108
11
votes
2 answers

HttpContent.ReadAsStringAsync causes request to hang (or other strange behaviours)

We are building a highly concurrent web application, and recently we have started using asynchronous programming extensively (using TPL and async/await). We have a distributed environment, in which apps communicate with each other through REST APIs…
alextercete
  • 4,871
  • 3
  • 22
  • 36
11
votes
2 answers

MessageQueue and Async / Await

I only want to receive my message in a async method! and its freezing my UI public async void ProcessMessages() { MessageQueue MyMessageQueue = new MessageQueue(@".\private$\MyTransactionalQueue"); MyMessageQueue.Formatter =…
Fraga
  • 1,361
  • 2
  • 15
  • 47
11
votes
2 answers

Async WebApi ActionFilterAttribute. An asynchronous module or handler completed while an asynchronous operation was still pending

I understand await waits for a task (an awaitable) to complete. But I'm confused about what that actually means. The code that doesn't work: public async override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { if…
lapsus
  • 2,915
  • 2
  • 32
  • 61
11
votes
1 answer

C# async, await without tasks

By creating one or more awaiters and awaitables, is it possible to build coroutines in C#? Ideally I would like to be able to write something like: void async Click() { var mouse_position = await left_mouse_click(); await…
Giuseppe Maggiore
  • 2,011
  • 1
  • 23
  • 31
11
votes
2 answers

Why must "await" be inside an "async" method?

Possible Duplicate: Why does the async keyword exist I have two methods. One is a normal method (MyMethod) and one is an async method (MyMethodAsync). I get a compilation error. static string MyMethod() { var result = await MyMethodAsync();…
Alex Yeung
  • 2,495
  • 7
  • 31
  • 48
11
votes
1 answer

How can you await a Task when you can't await

I'm developing a Windows 8 Runtime Component so the public interface can't contain Task as it's not a windows runtime type. This means I can't mark the method as async and can't await the private async methods in my library. This is leading to…
BenCr
  • 5,991
  • 5
  • 44
  • 68
11
votes
3 answers

ASP.NET 4.5 async-await and Response.Redirect

Is there any way to redirect from Page_Load (or any other ASP.NET event) when using async-await? Of course Redirect throws ThreadAbortException but even if I catch it with try-catch it ends up with an error page. If I call Response("url", false) it…
JakubRi
  • 819
  • 9
  • 11
11
votes
1 answer

async/await with ConfigureAwait's continueOnCapturedContext parameter and SynchronizationContext for asynchronous continuations

I would like put the code first and then explain the situation and ask my question based on that: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private async void…
11
votes
3 answers

Proper way to use Async with VS 2010 now that VS 2012 is released?

Due to work restrictions, I need to continue using Visual Studio 2010 for the immediate future. At the same time, I have been learning about Async in my personal coding. Is the latest Async CTP fully consistent with the Async language features of C#…
11
votes
3 answers

How to use Task.WhenAll() correctly

I am trying to use Task.WhenAll to await completion of multiple tasks. My code is below - it is supposed to launch multiple async tasks, each of which retrieves a bus route and then adds them to a local array. However, Task.WhenAll(...) returns…
Carlos P
  • 3,928
  • 2
  • 34
  • 50
11
votes
2 answers

Using async await inside void method

I have method with signature I cannot change. It should be protected override void OnInitialize() Using Windows 8 Metro API I need to check if file exists and read it, inside this NoSignatureChange method. Using PlainOldCSharp, I would write…
Alex Sorokoletov
  • 3,102
  • 2
  • 30
  • 52
10
votes
1 answer

Replacing Socket.ReceiveAsync with NetworkStream.ReadAsync (awaitable)

I have an application that makes a couple hundred TCP connections at the same time, and receives a constant stream of data from them. private void startReceive() { SocketAsyncEventArgs e = new SocketAsyncEventArgs(); e.Completed…
Josh
  • 2,083
  • 5
  • 23
  • 28