是什么为WaitAll和WhenAll之间的区别? [英] What is the difference between WaitAll and WhenAll?

查看:642
本文介绍了是什么为WaitAll和WhenAll之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

 列表< ComponentesClasificaciones> misClasificaciones =新的List< ComponentesClasificaciones>();
            任务tskClasificaciones = Task.Run(()=>
                {
                    misClasificaciones = VariablesGlobales.Repositorio.buscarComponentesClasificacionesTodosAsync()结果。
                });Task.WhenAll(tskClasificaciones);清单< ComponentesClasificaciones> misVClasificacionesParaEstructuras =新的List< ComponentesClasificaciones>(misClasificaciones);

如果我用 Task.WhenAll misClasificaciones 没有任何元素,但是当我使用爱维特我得到的所有我要求到数据库的元素。

在使用 WhenAll ,何时使用为WaitAll


解决方案

MSDN地解释这方面做得很好。所不同的是pretty毫不含糊。

Task.WhenAll:


  

创建一个任务,当所有的提供的任务已经完成,将完成。


Task.WaitAll:


  

所有提供的任务等待对象完成执行。


所以,从本质上讲, WhenAll 给你未直到这一切都完成你给的任务完成的任务(并允许程序执行立即继续)而为WaitAll 仅数个街区,并等待所有的任务,你传递结束。

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:

Creates a task that will complete when all of the supplied tasks have completed.

Task.WaitAll:

Waits for all of the provided Task objects to complete execution.

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), whereas WaitAll just blocks and waits for all of the tasks you pass to finish.

这篇关于是什么为WaitAll和WhenAll之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆