Await & Async in C# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Await & Async in C#

Could someone explain to me what Await and Async are and what they are used for? Please be clear, I've read a lot on the web but it's still not clear, maybe I skipped something before like Tasks.

6th Jul 2021, 2:51 PM
Mick
Mick - avatar
5 Answers
+ 9
Mick - Such a great question. However, in this community... it's tough to know if you're asking as a beginner or if you have some experience and are trying to dig deeper. I'm going to respond as if it's the latter and provide a bit of a primer to navigate the complexities of this advanced concept. ---- First, review the following article as a great starting point for digging deeper into this topic. https://www.sitepoint.com/asynchronous-programming-using-async-await-in-c/ In particular, pay special attention to the following sections: - Concurrent vs Parallel vs Asynchronous - Asynchronous Programming - Task and Task<T> - async and await The rest of the article is just fantastic and goes much deeper. However, it might be a bit more advanced for many. So, it's up to you how much deeper you want to explore about the voodoo magic happening in the compiler for methods marked with the async modifier.
7th Jul 2021, 6:39 AM
David Carroll
David Carroll - avatar
+ 8
Additional details to keep in mind when reviewing the link above: - async / await further simplifies the TAP (Task Asynchronous Programming) model. - The TAP model makes uses of the TPL (Task Parallel Library) - The TAP model is referred to as a language level asynchronous model, which is essentially based on the concept of futures and promises (https://en.wikipedia.org/wiki/Futures_and_promises). - This is in contrast to the more complicated traditional techniques working directly with threads, callbacks, and events. - While the TAP model is built on top of TPL, async / await are language constructs for which the compiler transforms code into a state machine for yielding and resuming execution at the point await is reached. - While the state machine code can be seen when compiled as IL, the usage of async / await makes the code appear much more simplified as if it was synchronous. Hopefully, these details will serve as a primer for keeping things straight while reading about async / await.
7th Jul 2021, 6:40 AM
David Carroll
David Carroll - avatar
+ 6
Synchronized programs run code sequentially. A then B then C. Nothing else can happen until C is done and B can't be done until A gets done ect... For applications, this can mean that the UI is unresponsive while processing A, B, and C. If they each take 3 seconds, the UI will lock up for 9 seconds. Async allows you to kick off A while still allowing the application to be responsive. If B and C don't rely on A, then you can kick all of them off at the same time and give control back to the user interface. These processes are running at the same time so, they could all finish in 3 second, not 9. If you require the results of A, you can await before you go on to the next step in the process but, this does not lock up the UI or prevent other async tasks from starting.
6th Jul 2021, 9:42 PM
Kail Galestorm
Kail Galestorm - avatar
0
So it works even without implementing the Thread class? Do you have any links that simply show your example but applied? That could be pretty useful. The answer is very comprehensive
6th Jul 2021, 11:39 PM
Mick
Mick - avatar
0
Quite thorough as a guide
7th Jul 2021, 8:13 AM
Mick
Mick - avatar