Skip to content

Cancellation patterns#52996

Open
BillWagner wants to merge 4 commits intodotnet:mainfrom
BillWagner:async-aweigh-4
Open

Cancellation patterns#52996
BillWagner wants to merge 4 commits intodotnet:mainfrom
BillWagner:async-aweigh-4

Conversation

@BillWagner
Copy link
Copy Markdown
Member

@BillWagner BillWagner commented Apr 13, 2026

Contributes to #17714

  1. Create cancel-non-cancelable-async-operations.md — from "How do I cancel non-cancelable async operations?" Covers the 3 meanings of cancellation, the WithCancellation<T> extension method pattern using Task.WhenAny. Update: note that many .NET Core streams are now fully cancelable; NetworkStream async operations honor tokens.
  2. Create coalescing-cancellation-tokens-from-timeouts.md — from "Coalescing CancellationTokens from timeouts." Covers combining a user-provided CancellationToken with timeout-based tokens using CancellationTokenSource.CreateLinkedTokenSource. Update for modern API surface.
  3. Add both to TOC near existing cancellation content.

Internal previews

📄 File 🔗 Preview link
docs/navigate/advanced-programming/toc.yml docs/navigate/advanced-programming/toc
docs/standard/asynchronous-programming-patterns/cancel-non-cancelable-async-operations.md Cancel non-cancelable async operations
docs/standard/asynchronous-programming-patterns/coalesce-cancellation-tokens-from-timeouts.md "Coalesce cancellation tokens from timeouts"

Contributes to dotnet#17714

1. Create `cancel-non-cancelable-async-operations.md` — from "How do I cancel non-cancelable async operations?" Covers the 3 meanings of cancellation, the `WithCancellation<T>` extension method pattern using `Task.WhenAny`. **Update:** note that many .NET Core streams are now fully cancelable; `NetworkStream` async operations honor tokens.
1. Create `coalescing-cancellation-tokens-from-timeouts.md` — from "Coalescing CancellationTokens from timeouts." Covers combining a user-provided `CancellationToken` with timeout-based tokens using `CancellationTokenSource.CreateLinkedTokenSource`. **Update** for modern API surface.
1. Add both to TOC near existing cancellation content.
@BillWagner BillWagner marked this pull request as ready for review April 14, 2026 21:06
@BillWagner BillWagner requested a review from a team as a code owner April 14, 2026 21:06
Copilot AI review requested due to automatic review settings April 14, 2026 21:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds two new conceptual docs on modern .NET cancellation patterns, plus runnable C# and Visual Basic snippets, and links the new docs from the advanced programming TOC.

Changes:

  • Add a doc and snippets for canceling non-cancelable async operations, including the WithCancellation<T>/Task.WhenAny “cancel the wait” pattern, and guidance for observing late faults.
  • Add a doc and snippets for coalescing caller cancellation with timeout cancellation via CancellationTokenSource.CreateLinkedTokenSource, plus a WaitAsync timeout alternative.
  • Link both new docs in docs/navigate/advanced-programming/toc.yml.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
docs/standard/asynchronous-programming-patterns/cancel-non-cancelable-async-operations.md New conceptual article describing cancellation semantics and patterns, with snippet references.
docs/standard/asynchronous-programming-patterns/coalesce-cancellation-tokens-from-timeouts.md New conceptual article describing linked token sources and timeout alternatives, with snippet references.
docs/navigate/advanced-programming/toc.yml Adds the two new articles to the advanced programming TOC.
docs/standard/asynchronous-programming-patterns/snippets/cancel-non-cancelable-async-operations/csharp/Program.cs C# runnable snippets for token-aware APIs, WithCancellation<T>, cancel-both, and late-fault observation.
docs/standard/asynchronous-programming-patterns/snippets/cancel-non-cancelable-async-operations/csharp/CancelNonCancelableAsyncOperations.csproj C# project file for compiling the new snippets.
docs/standard/asynchronous-programming-patterns/snippets/cancel-non-cancelable-async-operations/vb/Program.vb VB runnable snippets mirroring the C# examples.
docs/standard/asynchronous-programming-patterns/snippets/cancel-non-cancelable-async-operations/vb/CancelNonCancelableAsyncOperations.vbproj VB project file for compiling the new snippets.
docs/standard/asynchronous-programming-patterns/snippets/coalescing-cancellation-tokens-from-timeouts/csharp/Program.cs C# runnable snippets for linked CTS timeouts and WaitAsync timeout semantics.
docs/standard/asynchronous-programming-patterns/snippets/coalescing-cancellation-tokens-from-timeouts/csharp/CoalescingCancellationTokensFromTimeouts.csproj C# project file for compiling the new snippets.
docs/standard/asynchronous-programming-patterns/snippets/coalescing-cancellation-tokens-from-timeouts/vb/Program.vb VB runnable snippets mirroring the C# linked CTS and WaitAsync examples.
docs/standard/asynchronous-programming-patterns/snippets/coalescing-cancellation-tokens-from-timeouts/vb/CoalescingCancellationTokensFromTimeouts.vbproj VB project file for compiling the new snippets.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@BillWagner BillWagner requested a review from adegeo April 14, 2026 21:16
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.

2 participants