Why should I prefer single 'await Task.WhenAll' over multiple awaits?
Yes, use WhenAll
because it propagates all errors at once. With the multiple awaits, you lose errors if one of the earlier awaits throws.
Another important difference is that WhenAll
will wait for all tasks to complete even in the presence of failures (faulted or canceled tasks). Awaiting manually in sequence would cause unexpected concurrency because the part of your program that wants to wait will actually continue early.
I think it also makes reading the code easier because the semantics that you want are directly documented in code.
My understanding is that the main reason to prefer Task.WhenAll
to multiple await
s is performance / task "churning": the DoWork1
method does something like this:
- start with a given context
- save the context
- wait for t1
- restore the original context
- save the context
- wait for t2
- restore the original context
- save the context
- wait for t3
- restore the original context
By contrast, DoWork2
does this:
- start with a given context
- save the context
- wait for all of t1, t2 and t3
- restore the original context
Whether this is a big enough deal for your particular case is, of course, "context-dependent" (pardon the pun).