Skip to content

IAsyncEnumerator with WithCancellation extension method #111758

Answered by stephentoub
ladeak asked this question in General
Discussion options

You must be logged in to vote

the compiler does not seem to generate an iterator that would preemptively stop the iteration when the token gets in the canceled state. However, when I look at the code the extension method gives me the impression that this would happen (because the token is passed through the iterator).

The sole purpose of WithCancellation is to pass the provided token into the GetAsyncEnumerator method. That's it, nothing more, nothing less.

With [EnumeratorCancellation], the compiler then generates an implementation for the async iterator that ensures the CancellationToken passed into GetAsyncEnumerator is passed into the body of the iterator as the tagged CancellationToken parameter. Again, nothing…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ladeak
Comment options

Answer selected by ladeak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants