3

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?

Ygalbel
  • 5,214
  • 1
  • 24
  • 32
  • 2
    You should never use Task.Result. It is bad practice. – Nolan Bradshaw Oct 08 '20 at 18:04
  • 10
    @NolanBradshaw except in cases like this, when the tasks are *already* finished – Panagiotis Kanavos Oct 08 '20 at 18:05
  • 3
    @PanagiotisKanavos Correct, missed the await on Task.WhenAll(). Thank you for correcting. – Nolan Bradshaw Oct 08 '20 at 18:06
  • 3
    Related: [Await on a completed task same as task.Result?](https://stackoverflow.com/questions/24623120/await-on-a-completed-task-same-as-task-result) You may also find this interesting: [Is Task.Result the same as .GetAwaiter.GetResult()?](https://stackoverflow.com/questions/17284517/is-task-result-the-same-as-getawaiter-getresult) – Theodor Zoulias Oct 08 '20 at 18:13

1 Answers1

6

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

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • You seems to be right but I want to be sure 100% percent – Ygalbel Oct 08 '20 at 18:41
  • 2
    I agree with this answer-- it's perfectly okay to use `.Result` after the task has been awaited. The only thing I would add is that it is also okay to `await` it again, which would give you the same result, and might be safer. – John Wu Oct 08 '20 at 18:43
  • As duplicate shows there is indeed differences in cases explicitly not covered in this answer- when Result doesn’t produce result. @ygalbel make sure to check duplicate too – Alexei Levenkov Oct 08 '20 at 18:56
  • 1
    `await` isn't free. [Check the issue raised about the analyzer warning](https://github.com/microsoft/vs-threading/issues/541) , you'll see that the extra awaits can increase the application size due to the state machine code : `I also work on the Cortana UWP app and adding four extra await's adds 10-20KB to the app size which is to be avoided. It also hits performance a little.` – Panagiotis Kanavos Oct 09 '20 at 05:43
  • @PanagiotisKanavos You are wrong, if you read the whole thread you linked, it is concluded that he made a mistake when analyzing app size - and they agreed that an extra await is indeed the best way. – AnorZaken Dec 13 '22 at 13:20