In short, Which one is the best among the above two constructs ?
You will know the impact, when system scalability comes in the picture, difference between Task.Wait()
and await Task
is way they treat the calling / executing thread. When the first one would block till the completion happens, other one using await
would free up the caller, but mind it only for a True Asynchronous call, which would mean a IO call, Network call, Database call which is not in memory logical processing, and expose Async APIs, that's when calling thread or the context is freed up. For a true Async call, it would use IO completion ports (queuing mechanism) in windows, to execute the Async pipeline.
This would not happen if at the end of the call chain final method is not true Async.
They look almost similar Wait
and await
Indeed, for both continuation will execute post completion, but let's take an example, normally threads getting processed are directly linked to the number of cores in the system, using TPL these thread are invoked from a pool and there are limit to number of thread available. For a high scalable system like 100 K / sec requests, Task.Wait() would time out for a long running method very quickly, as threads are busy idling around and blocked and they will not take new request, but for await no such blockage happens at all.
Practical examples
Javascript, NodeJs always go for Async requests by default, no blocking, which is reason why the systems are highly scalable
Also consider ConfigureAwait(false)
, along with await, then it will not look for same thread context to re-enter for the continuation to execute, which is much more efficient