Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Result or await after Task.WhenAll [duplicate]

I have two tasks, and I wait them to be finished with

await Task.WhenAll

Can it be a problem to get after that the value directly by calling .Result

I am sure that the tasks are already finished.

`

        Task<int> t1 = Task.FromResult(1);
        Task<int> t2 = Task.FromResult(2);
        await Task.WhenAll(t1, t2);
        var uuu = t1.Result; 
        // or var uuu = await t1;

`

The problem is that visual studio set a VSTHRD103 Call async methods when in an async method warning.

I checked with sharplab and .Result version jit is a bit smaller.

Can I get any deadlock if I call .Result after that I already await them with Task.WhenAll?

like image 594
Ygalbel Avatar asked Feb 03 '26 01:02

Ygalbel


1 Answers

There's no problem, as the tasks have already finished. This is a false positive which can be ignored.

If all tasks return the same result though, Task.WhenAll returns an array with the results:

var results=await Task.WhenAll(tasks);

The results are in the same order as the tasks that produced them

like image 178
Panagiotis Kanavos Avatar answered Feb 04 '26 14:02

Panagiotis Kanavos