How to assign the results of WaitAll tasks to variables [duplicate]
Asked Answered
B

2

5

I have a situation like this:

var retrievalTasks = new Task[2];

retrievalTasks[0] = GetNodesAsync();

retrievalTasks[1] = GetAssetsToHandleAsync();

Task.WaitAll(retrievalTasks);

And I would like the result of retrievalTasks[0] and retrievalTasks[1] to be stored in variables.

I could achieve this with:

 var a = await GetNodesAsync();
 var b = await GetAssetsToHandleAsync();;

But I would rather not await both like that, because then they're not fired at the same time right? Or am I misunderstanding?

I've seen this as a reference: Awaiting multiple Tasks with different results

But I think this is a slightly different scenario that wouldn't work in my case.

Any ideas?

Thanks

EDIT:

await Task.WhenAll(catTask, houseTask, carTask);

var cat = await catTask;

var house = await houseTask;

var car = await carTask;

This just seemed to wait four times to get the three results. However, @armenm has shown how to avoid that.

Bibliolatry answered 11/6, 2018 at 9:55 Comment(9)
Return type of both the functions should be same.Mitchelmitchell
What makes you think the one you've found doesn't work in your case?Takeshi
Because the answer is waiting all individually, i.e. await a, await bBibliolatry
which makes me think they won't be fired at the same timeBibliolatry
That's just syntactic stuff at that point, they've been awaited in the WhenAll, in practice all the following awaits will run synchronouslyTakeshi
so are they awaiting twice in that example, or is it because it's already awaited it won't make a difference?Bibliolatry
Read through the comments etc under the answers - they go into more detail than is worth repeating here. If you still think it doesn't work for you, please edit your question with reasoning as to why not.Takeshi
I read through those comments, but it just looked like it would still be different. Maybe it's just my misunderstanding. However, this answer below is more suited to what I was attempting I think.Bibliolatry
nuget.org/packages/TaskTupleAwaiterBeaudoin
I
10

Here we go, you need to have separate variables for the tasks having respective types:

var task1 = GetNodesAsync();
var task2 = GetAssetsToHandleAsync();

Task.WaitAll(new Task[] { task1, task2 });

var result1 = task1.Result;
var result2 = task2.Result;

But I would recommend to make it asynchronous:

var task1 = GetNodesAsync();
var task2 = GetAssetsToHandleAsync();

await Task.WhenAll(new Task[] { task1, task2 });

var result1 = task1.Result;
var result2 = task2.Result;
Iguanodon answered 11/6, 2018 at 10:7 Comment(1)
Thank you. I was trying to use result before. But I think the way I had it set up wouldn't allow it.Bibliolatry
M
-1

You can also wrap the methods in Task.Run to avoid using task variables. For example:

private async Task DoStuff()
{
    int i1, i2;

    await Task.WhenAll(
        Task.Run(async () => i1 = await Calc1Async()),
        Task.Run(async () => i2 = await Calc2Async()));
}
Menstruum answered 10/7 at 17:42 Comment(5)
Why choose to reopen a 6 year old question with a shockingly wrong answer?Turret
@Turret questions here are not closed after a period of inactivity. This is not Microsoft's GitHub. It's perfectly fine to answer a 15 years old question.Dusky
@Turret also would you like to explain why you think that this answer is wrong?Dusky
@TheodorZoulias It makes no sense to add Task.Run as a wrapper around method calls that are already async. The original question doesn't even state that the return types are integers.Turret
@Turret you said that the answer is wrong. Shockingly wrong to be precise. The Task.Run doesn't affect the correctness of the operation. At least not in general. There are only a couple of cases that wrapping the asynchronous calls in Task.Run would compromise the correctness of the operation, namely the Calc1Async and Calc2Async including non-thread-safe side-effects, or including interactions with thread-affine components. The Task.Run could (potentially) affect the performance of the combined operation in a positive way. I see nothing shockingly wrong with this answer.Dusky

© 2022 - 2024 — McMap. All rights reserved.