This question already has an answer here:
- WaitAll vs WhenAll 4 answers
I have this code:
List<ComponentesClasificaciones> misClasificaciones = new List<ComponentesClasificaciones>();
Task tskClasificaciones = Task.Run(() =>
{
misClasificaciones = VariablesGlobales.Repositorio.buscarComponentesClasificacionesTodosAsync().Result;
});
Task.WhenAll(tskClasificaciones);
List<ComponentesClasificaciones> misVClasificacionesParaEstructuras = new List<ComponentesClasificaciones>(misClasificaciones);
If I use Task.WhenAll
, misClasificaciones
does not have any element but when I use awit all I get all the elements that I request to the database.
When to use WhenAll
and when to use WaitAll
?
MSDN does a good job of explaining this. The difference is pretty unambiguous.
Task.WhenAll:
Task.WaitAll:
So, essentially,
WhenAll
gives you a task that isn't done until all of the tasks you give it are done (and allows program execution to continue immediately), whereasWaitAll
just blocks and waits for all of the tasks you pass to finish.WhenAll
returns a task that you canContinueWith
once all the specified tasks are complete. You should be doingBasically, use
WaitAll
when you want to synchronously get the results, useWhenAll
when you want to start a new asynchronous task to start some more processing