从Task.WhenAll获取结果 [英] Get result from Task.WhenAll

查看:71
本文介绍了从Task.WhenAll获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个任务返回要使用 Task.WhenAll(new [] {t1,t2,t3}); 调用的对象类型,并读取结果.

I have multiple tasks returning the same object type that I want to call using Task.WhenAll(new[]{t1,t2,t3}); and read the results.

当我尝试使用

Task<List<string>> all = await Task.WhenAll(new Task[] { t, t2 }).ConfigureAwait(false);

我收到编译器错误

无法将类型'void'隐式转换为'System.Threading.Tasks.Task< System.Collections.Generic.List< string>>

两个任务都调用与此类似的方法.

both tasks are calling method similar the this.

private Task<List<string>> GetFiles(string path)
{
    files = new List<string>();
    return  Task.Run(() =>
    {
       //remove for brevity 
        return files;
    });
}

推荐答案

好像您正在使用不返回值的 WaitAll()的重载.如果您进行以下更改,则应该可以使用.

Looks like you are using the overload of WaitAll() that doesn't return a value. If you make the following changes, it should work.

List<string>[] all = await Task.WhenAll(new Task<List<string>>[] { t, t2 })
                               .ConfigureAwait(false);

这篇关于从Task.WhenAll获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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