What are Async and Await in C# ?
- Async and Await keywords are used to create asynchronous methods in C.
- Asynchronous programming means that the process runs independently of main or other processes.
Usage of Async and Await is as shown below:
- Async keyword is used for the method declaration.
- The count is of a task of type int which calls the method CalculateCount().
- Calculatecount() starts execution and calculates something.
- Independent work is done on my thread and then await count statement is reached.
- If the Calculatecount is not finished, myMethod will return to its calling method, thus the main thread doesn’t get blocked.
- If the Calculatecount is already finished, then we have the result available when the control reaches await count. So the next step will continue in the same thread. However, it is not the situation in the above case where Delay of 1 second is involved.
No comments