Reputation: 11
In the task-based asynchronous pattern - while calling a method we use the await
keyword, i.e.;
await client.OperationName(parameterlist)
The await
keyword suspends the execution of the method until the awaited task completes.
"AWAIT SUSPENDS THE EXECUTION OF THE METHOD"
Then how does it differ from synchronous calling?
Upvotes: 1
Views: 186
Reputation: 5312
I think the term "suspends" is a bit confusing. To be more precise - calling await on an async method yields the execution to the calling method, which means it won't wait for the method to finish executing, and won't block the thread. Once it's done executing in the background, the method will continue from where it stopped.
With a synchronous method - the thread's execution won't continue until the method finishes executing, which will block.
From MSDN:
Async methods are intended to be non-blocking operations. An await expression in an async method doesn’t block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.
Read Stephen Cleary's articles on this stuff. They are very informative and should clear up any confusion or questions you have.
http://blog.stephencleary.com/2012/02/async-and-await.html
http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
Upvotes: 1
Reputation: 5234
In a synchronous scenario, if a method is long-running the thread blocks while waiting for the method's execution to complete. This may lead to scalability/performance issues. Conversely, in an asynchronous scenario (async/await
) the thread is released until the awaitable part(s) of the method has completed.
This is the awaitable part of your method. The method's execution is suspended here until the await is complete.
await client.OperationName(parameterlist)
Upvotes: 0