Skip to content

Clarify Task.WhenAny documentation to address user confusion about Task<Task> return type #48051

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

Merged
merged 2 commits into from
Aug 22, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 20, 2025

The documentation for Task.WhenAny in the async programming guide was causing confusion for readers who couldn't understand the difference between "the two tasks" mentioned in the explanation. The issue was that the explanation didn't clearly distinguish between:

  1. The wrapper Task<Task> returned by Task.WhenAny
  2. The actual completed task contained within that wrapper

Changes Made

Updated the explanation in the "Apply await expressions to tasks efficiently" section to more clearly explain:

  • That Task.WhenAny returns a Task<Task> - a wrapper task that contains the completed task
  • The difference between awaiting the wrapper task vs awaiting the actual completed task
  • Why you need to await the completed task again (to access results and handle exceptions properly)

Before

The `await Task.WhenAny` expression doesn't wait on the finished task, but rather waits on the `Task` object returned by the `Task.WhenAny` method. The result of the `Task.WhenAny` method is the completed (or faulted) task.

After

This line is important because `Task.WhenAny` returns a `Task<Task>` - a wrapper task that contains the completed task. When you `await Task.WhenAny`, you're waiting for the wrapper task to complete, and the result is the actual task that finished first.

The new explanation uses clearer language and explicitly mentions the Task<Task> wrapper concept to help readers understand why there are "two tasks" involved and why both await operations are necessary.

Fixes #44497.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/asynchronous-programming/index.md docs/csharp/asynchronous-programming/index

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Confusion with Task.WhenAny Clarify Task.WhenAny documentation to address user confusion about Task<Task> return type Aug 20, 2025
@Copilot Copilot AI requested a review from BillWagner August 20, 2025 21:40
Copilot finished work on behalf of BillWagner August 20, 2025 21:40
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good and is ready for final review.

@BillWagner BillWagner marked this pull request as ready for review August 21, 2025 15:00
@BillWagner BillWagner requested a review from a team as a code owner August 21, 2025 15:00
@BillWagner BillWagner requested a review from adegeo August 21, 2025 15:00
@BillWagner BillWagner enabled auto-merge (squash) August 22, 2025 18:14
@BillWagner BillWagner merged commit ea8459a into main Aug 22, 2025
20 checks passed
@BillWagner BillWagner deleted the copilot/fix-44497 branch August 22, 2025 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Confusion with Task.WhenAny
3 participants