The async keyword of C# is used to qualify that a method,
lambda expression, or anonymous method should be called in an asynchronous manner automatically.
For this, the CLR will create a new thread of
execution to handle the task at hand. Furthermore, when you are calling an async method, the await
keyword will automatically pause the current thread from any further activity until the task is
complete, leaving the calling thread free to continue on its merry way.
1.Methods (as well as lambda expressions or anonymous methods) can be marked with the async keyword to enable the method to do work in a non-blocking manner e.g. in the below figure, The method ProcessSleep() is marked with async
async keyword will run in a blocking manner until the await keyword is encountered. e.g. in the above figure, The method will run in a blocking manner until the await sleepingClass.DoSleep() statement is encountered
3.A single async method can have multiple await contexts. e.g. the above method can multiple await calls
4.When the await expression is encountered, the calling thread is suspended until the awaited task is complete. In the meantime, control is returned to the caller of the method. e.g. In the above method ProccessSleep(), When await expression is encountered, The calling thread i.e. The main method thread is suspended until the awaited task is complete.In meantime, Control is returned to the Main method to continue executing its statements
5.The await keyword will hide the returned Task object from view, appearing to directly return the underlying return value. Methods with no return value simply return void.
6.As a naming convention, methods that are to be called asynchronously should be marked with the “Async” suffix..e.g. In case, they are SleepingClass.DoSleep() and ProcessSleep()
No comments:
Post a Comment