What is the difference between WaitAll and WhenAll?

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, everything gets all the elements that I query in the database.

When to use WhenAll and when to use WaitAll ?

+5
source share
2 answers

MSDN explains this pretty well. The difference is pretty straightforward.

Task.WhenAll :

Creates a task that will be completed after all tasks have been completed.

Task.WaitAll :

Expects all provided task objects to complete.

Thus, essentially, WhenAll gives you a task that is not executed until all the tasks you have completed are completed (and so that the program continues immediately), while WaitAll simply blocks and waits for all the tasks that you pass, end .

+11
source

WhenAll returns a job that you can ContinueWith after completing all the tasks given. You have to do

 Task.WhenAll(tskClasificaciones).ContinueWith(t => { // code here }); 

Basically, use WaitAll when you want to get results synchronously, use WhenAll when you want to start a new asynchronous task to start some more processing

+10
source

Source: https://habr.com/ru/post/1205506/


All Articles