Await & Async in C# | Sololearn: Learn to code for FREE!

+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.

7/6/2021 2:51:37 PM

Mick

5 Answers

New Answer

+7

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.

+6

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.

+5

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.

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

0

Quite thorough as a guide