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
5 answers

await for a PushModalAsync form to closed in xamarin forms

I have a page and on clicking a plus button on toolbar i am calling a popup page from popup page user can add a new entry or cancel / close window without doing anything Everything is working fine and code is like this public partial class…
Sebastian
  • 4,625
  • 17
  • 76
  • 145
11
votes
1 answer

Maximum number of Threads available to Tasks

I'm Trying to get my head around the async-await functionality within C#. I've written the below code to run several tasks asynchronously - currently all they do is raise an event after a certain amount of time. public class Program { public…
C. Knight
  • 739
  • 2
  • 5
  • 20
11
votes
1 answer

What should happen with `await` when the expression after the keyword does not evaluate to promise?

I have a ES7 code like this. async function returnsfive() { var three = 3; var threeP = await three; return threeP+2; } returnsfive().then(k=>console.log(k), e=>console.error("err", e)) What should happen at the var threeP = await three…
Karel Bílek
  • 36,467
  • 31
  • 94
  • 149
11
votes
2 answers

How do do an async ServiceController.WaitForStatus?

So ServiceController.WaitForStatus is a blocking call. How can it be done Task/Async manner?
Simon
  • 33,714
  • 21
  • 133
  • 202
11
votes
1 answer

Async call to WCF client blocks subsequent synchronous calls

I'm seeing a problem with WCF when calling the generated Async methods on the client... If I await on an async method, and then subsequently call a non-async method on the same client, the blocking method never returns. Here's a trivial reproduction…
Mark
  • 1,784
  • 16
  • 26
11
votes
1 answer

Check the Number of threads available in ThreadPool at any time

Where and how to check through code number of threads available in threadpool. What i want is: For example if no of threads in threadpool are 10 then at any given time I should not be using say more than 3 threads. Once an existing thread gets…
dotnetavalanche
  • 804
  • 2
  • 12
  • 25
11
votes
1 answer

Python asyncio wait for threads

I have a situation where I have a "server" thread which should listen calls/events from other server threads while at the same time executing some other code. Lately I have worked a lot with Node.js so I thought that it would be nice to use…
Tumetsu
  • 1,661
  • 2
  • 17
  • 29
11
votes
2 answers

Interrupt all asyncio.sleep currently executing

where This is on Linux, Python 3.5.1. what I'm developing a monitor process with asyncio, whose tasks at various places await on asyncio.sleep calls of various durations. There are points in time when I would like to be able to interrupt all said…
tzot
  • 92,761
  • 29
  • 141
  • 204
11
votes
3 answers

Synchronous I/O within an async/await-based Windows Service

Let's say I have a Windows Service which is doing some bit of work, then sleeping for a short amount of time, over and over forever (until the service is shut down). So in the service's OnStart, I could start up a thread whose entry point is…
Bob Vesterman
  • 1,127
  • 1
  • 11
  • 31
11
votes
2 answers

Task.WhenAll not waiting

I am learning how to use async functions in console application but can't make the Task.WhenAll wait until all tasks are completed. What is wrong with the following code? It works synchronously. Thank you in advance. static void Main(string[]…
Gabriel
  • 157
  • 1
  • 2
  • 7
11
votes
4 answers

Should C# Asynchronous Library Methods Call await?

Should an asynchronous library method call await? For example, assume I have a data services library method that has access to an Entity Framework 6 data context named 'repository'. As far as I can see, I have two ways of defining this…
Neilski
  • 4,385
  • 5
  • 41
  • 74
11
votes
3 answers

Async Progress Bar Update

I am trying to use an async await to update a progress bar on my WinForm based on a copy operation, but the progress bar will only update when the Copy function has finished, and then throws an exception that it can't update as it's not on the same…
Bali C
  • 30,582
  • 35
  • 123
  • 152
11
votes
1 answer

IDisposable.Dispose() not called in Release mode for async method

I wrote the following WPF sample app in VB.NET 14 using .NET 4.6.1 on VS2015.1: Class MainWindow Public Sub New() InitializeComponent() End Sub Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs) …
Nick Muller
  • 2,003
  • 1
  • 16
  • 43
11
votes
2 answers

TaskAwaiter does not implement INotifyCompletion When Using Visual Studio 2015

I have an old ASP.NET Web Form project that I need to add an async library too. I've upgraded it to .NET Framework v4.5.1. It run and compiles perfectly with Visual Studio 2013. However, when I open it with Visual Studio 2015, it wont compile.…
Dale Alleshouse
  • 1,627
  • 2
  • 17
  • 24
11
votes
1 answer

Implement Async Interface synchronous

Assume i have the following interface: public interface IApiOutputCache { Task RemoveStartsWithAsync(string key); Task Get(string key) where T : class; Task RemoveAsync(string key); Task ContainsAsync(string key); …
LaurinSt
  • 952
  • 11
  • 25