You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have several occurrences of the following pattern:
Task[]tasks= CreateTasks();await Task.WhenAll(tasks);foreach(var task in tasks)
Console.WriteLine(task.Result);
The above code will generate an AsyncFixer02 warning for task.Result. This seems to be not necessary, because the task will already be completed at this point.
What is the expected fix to this? I'm tired of suppressing the warning for every such place in our code.
The text was updated successfully, but these errors were encountered:
I can't really answer the question, but this may be equivalent for your code base and doesn't cause a warning:
Task[]tasks= CreateTasks();await Task.WhenAny(tasks);foreach(var task in tasks)
Console.WriteLine(await task);
This has the added benefit that if you do something with the results that takes non-trivial time in the foreach then in the worst case this will take as much time as the WhenAll() code, but can be quicker if the slowest task is not the first one.
Good to see you, @Piedone! Thanks for the suggestion - nice hack. Actually, using await task instead of task.Result will work just as well when using Task.WhenAll() - which is more expressive and less surprising, I guess. But the idea is interesting!
We have several occurrences of the following pattern:
The above code will generate an AsyncFixer02 warning for
task.Result
. This seems to be not necessary, because thetask
will already be completed at this point.What is the expected fix to this? I'm tired of suppressing the warning for every such place in our code.
The text was updated successfully, but these errors were encountered: