Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusion with Task.WhenAny #44497

Open
Ginomed opened this issue Jan 23, 2025 · 1 comment
Open

Confusion with Task.WhenAny #44497

Ginomed opened this issue Jan 23, 2025 · 1 comment

Comments

@Ginomed
Copy link

Ginomed commented Jan 23, 2025

Type of issue

Missing information

Description

Thank you for the great article.
I have a problem to understand the following passage:

Near the end, you see the line await finishedTask;. The line await Task.WhenAny doesn't await the finished task. It awaits the Task returned by Task.WhenAny. The result of Task.WhenAny is the task that has completed (or faulted).

Could you explain the difference between the two tasks? They have the same Id.
I understand what you say in the next sentence though.

You should await that task again, even though you know it's finished running. That's how you retrieve its result, or ensure that the exception causing it to fault gets thrown.

Page URL

https://learn.microsoft.com/de-de/dotnet/csharp/asynchronous-programming/

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/asynchronous-programming/index.md

Document Version Independent Id

3001f6f2-b04b-5513-54a6-fedfccc94e51

Article author

@BillWagner

Metadata

  • ID: 79c2a674-a335-4802-5c52-4a2af998cc7f
  • Service: dotnet-csharp
  • Sub-service: async-task-programming

Related Issues

@BillWagner
Copy link
Member

Hi @Ginomed

I agree that this is subtle. Here's how I think about it:

  • Task.WhenAny returns a Task<Task>. That task completes when one of the "any" tasks complete.
  • When you await Task.WhenAny(...), the unpackaged task is the first Task of the set completes.

We should rework this to make it more clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants