I need a clear Explanation About async and await in C# Can Anyone Elaborate deeply?
Loading
I need a clear Explanation About async and await in C# Can Anyone Elaborate deeply?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit MohantyPosted Aug 22, 2023, 5:48 AM
In C#, the async and await keywords provide a convenient way to work with asynchronous code in a more readable and organized manner.
The async keyword is used to mark a method as asynchronous. An asynchronous method contains one or more await expressions and usually returns a Task or Task object. This indicates that the method can execute asynchronously and won't block the calling thread while it's waiting for certain operations to complete.
The await keyword is used within an asynchronous method to pause the execution of that method until the awaited task is complete. The task being awaited must be an instance of a type that represents an asynchronous operation, such as Task, Task, or other awaitable types.
Here's an example:
In this example MainAsync & OperationAsync is marked as asynchronous with the async keyword. OperationAsync method includes a simulated delay using Task.Delay. Within MainAsync, the await OperationAsync() call will pause the execution of MainAsync until OperationAsync completes.
The async and await are not meant to make your code run faster; they're designed to make your application more responsive by better utilizing resources and allowing concurrent execution of tasks that might otherwise block the main thread.
Mohammad HussainPosted Aug 24, 2023, 4:32 AM
asyncandawait:async: Theasynckeyword is used to mark a method as asynchronous. This indicates that the method contains asynchronous operations and will likely use theawaitkeyword to manage them.await: Theawaitkeyword is used within anasyncmethod to pause the method's execution and asynchronously wait for the completion of an asynchronous operation. It allows the method to yield control back to the calling thread while the awaited operation is performed in the background.How
asyncandawaitWork Together: When you mark a method with theasynckeyword, you can useawaitto indicate points where you want to pause the method's execution and await the completion of asynchronous tasks. While awaiting a task, the calling thread is free to perform other tasks, making efficient use of system resources.Benefits:
Cr BhargaviPosted Aug 23, 2023, 1:18 PM
The async keyword is used to for asynchronous. An asynchronous method has more await expressions
The await keyword is used within an asynchronous method to pause the execution of that method until the awaited task is complete.
Vishal YelvePosted Aug 22, 2023, 7:32 AM
Hi Hari,
do refer below links
https://www.bytehide.com/blog/async-await-csharp
https://dotnettutorials.net/lesson/async-and-await-operator-in-csharp/
Kudvenket Video - https://www.youtube.com/watch?v=C5VhaxQWcpE
Shivprasad koirala Sir /Questpond video - https://www.youtube.com/watch?v=V2sMXJnDEjM&pp=ygUVYXN5bmMgYW5kIGF3YWl0IGluIGMj
Tahir AnsariPosted Aug 22, 2023, 6:41 AM
https://www.c-sharpcorner.com/article/async-and-await-in-c-sharp/
https://dotnettutorials.net/lesson/async-and-await-operator-in-csharp/