gpt4 book ai didi

c# - 我如何从WhenAll(任务数组)中获取结果

转载 作者:行者123 更新时间:2023-12-03 13:02:39 26 4
gpt4 key购买 nike

我有以下代码。

var tasks = new[]
{
Task.Factory.StartNew(() => GetSomething1()),
Task.Factory.StartNew(() => GetSomething2()),
Task.Factory.StartNew(() => GetSomething3())
};

var things = Task.WhenAll(tasks);

如何分别从所有三个任务中获得结果并打印?

最佳答案

为了分别获得结果,您可以通过多种方式来执行以下操作:

var task1  = GetSomething1();
var task2 = GetSomething2();
var task3 = GetSomething3();

// your method will continue when everything's completed, but you won't tie up a thread to just hang around until that time.
await Task.WhenAll(task1, task2, task3);

var result1 = await task1;
var result2 = await task2;
var result3 = await task3;

关于c# - 我如何从WhenAll(任务数组)中获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59394605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com