C# Async/Await/Task Explained (Deep Dive)
Why Stack Overflow recommends it
Stack Overflow users link it when explaining that async/await does not create threads and how the thread pool resumes a method after an await.
Recommended by 4 people across 3 different months, 2020 to 2022.
What it is
A 24-minute tutorial by Raw Coding that digs into how C# async/await works under the hood. It walks through the state machine the compiler generates for an async method, how await creates checkpoints in it, and how the Task class bridges code to the asynchronous model.
What people said
Recommends Welcor · 2020-05 · Stack Overflow · on “If async-await doesn't create any additional threads, then how does it make applications responsive?”
This does not directly answer the question, but I think it provides some interesting additional information: Async and await does not create new threads by itself. BUT depending on where you use async-await, the synchronous part BEFORE the await may run on a different thread than the synchronous part AFTER the await (for example ASP.NET and ASP.NET core behave differently). In UI-Thread based applications (WinForms, WPF) you will be on the same thread before and after. But when you use async-await on a Thread pool thread, the thread before and after the await may not be the same. A great video on this topic [this video]
Recommends Ivailo Manolov · 2021-05 · Stack Overflow · on “Why is "Using asynchronous [...] methods on CPU-bound [providing] no benefits and results in more overhead."”
Async programming is actually pretty vague to a lot of developers,me included.I always thought that async programming is concurrent,not parallel. Which means that we utilize it's waiting time in order to do something else and have the feel it is parallel. In reality however I stumbled upon this video couple of days ago.Link to video -> [this video] It goes quite in depth with the state machine looked in CIL.But what we are interested is the part where he shows that the first thread is actually busy spinning one loop.Time stamp is 7:50. You can clearly see that while the one thread was blocked now and the waiting time was over in the KettleBoiling method,the method actually finished while the one
Recommends Josh · 2021-05 · Stack Overflow · on “Should I use Task.Run() for BackgroundService that does I/O”
You're right, my misunderstanding was with the thread pool. After watching this: [this video] I see now that when you call await, the current async method saves its state, and the program continues doing whatever else it needed to. When the awaited method completes, the thread pool picks another thread (or the same one if that happened to be freed up in the meantime) and resets the state and allows the execution of the async function to continue. Because the thread pool manages that, it will not let you block the original thread after await has been called.
Recommends sofnomic cr · 2022-07 · Stack Overflow · on “I can't Task async and return bool”
You can do it. You can see this video to understand async/await pattern: [this video] . Try this: class Program { static async Task Main(string[] args) { bool result = await Tester(); System.Diagnostics.Debug.WriteLine($"result: {result}"); } private static bool completed = false; static async Task<bool> Tester() { completed = false; int iteration = 0; //var log = new Logger(); //log.Log(Logger.Level.Debug, "Starting"); try { await Task.Factory.StartNew(() => { while (!completed) { //log.Log(Logger.Level.Debug, "Waiting", 1); iteration++; System.Diagnostics.Debug.WriteLine($"iteration {iteration}"); Task.Delay(2000); if (iteration > 20) { completed = true; } } }); } catch (Exception e) { //log.Lo
Cites Gordon Khanh Ng. · 2021-08 · Stack Overflow · on “Is calling Task.Yield in Asp.Net Core ensuring continuation to run on the ThreadPool?”
They're difference, if you take the result as educational view point. Better explaination can be found here [LINK] , here [LINK] and... if you want to see how it compile to IL, take a shot here [this video] . From real world project, as i saw the only purpose and benefit from Task.Yield is to force a task to run asynchronous. (decorate a method as async / await doesn't ensure that they will run asynchronously, especially with the new ValueTask ). So, forcing immediate return the execution to the caller, means that you doesn't care it's final result (for example: you call an API, but never care about the response). Which according to me, in the asp.net core server side process, have a better appro