Skip to content

Commit d60a8c2

Browse files
authored
Explain semantics of multiple calls to CancellationTokenSource.Cancel (#11105)
1 parent eb2a87e commit d60a8c2

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

xml/System.Threading/CancellationTokenSource.xml

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -378,26 +378,30 @@
378378
<Docs>
379379
<summary>Communicates a request for cancellation.</summary>
380380
<remarks>
381-
<format type="text/markdown"><![CDATA[
382-
383-
## Remarks
384-
The associated <xref:System.Threading.CancellationToken> will be notified of the cancellation and will transition to a state where <xref:System.Threading.CancellationToken.IsCancellationRequested%2A> returns true.
381+
<format type="text/markdown"><![CDATA[
382+
383+
## Remarks
385384
386-
Any callbacks or cancelable operations registered with the <xref:System.Threading.CancellationToken> will be executed.
385+
The associated <xref:System.Threading.CancellationToken> is notified of the cancellation and transitions to a state where <xref:System.Threading.CancellationToken.IsCancellationRequested%2A> returns true.
386+
387+
Any callbacks or cancelable operations registered with the <xref:System.Threading.CancellationToken> are executed, if they haven't already been executed by a previous call to <xref:System.Threading.CancellationTokenSource.Cancel>. Subsequent calls to <xref:System.Threading.CancellationTokenSource.Cancel> won't execute the same callback again unless reregistered. (Avoid multiple calls to <xref:System.Threading.CancellationTokenSource.Cancel>, because the intent of such code is often unclear.)
387388
388-
We recommend that cancelable operations and callbacks registered with <xref:System.Threading.CancellationToken> not throw exceptions.
389+
Callbacks are executed synchronously in LIFO order.
389390
390-
This overload of Cancel will aggregate any exceptions thrown into an <xref:System.AggregateException>, such that one callback throwing an exception will not prevent other registered callbacks from being executed.
391+
We recommend that cancelable operations and callbacks registered with <xref:System.Threading.CancellationToken> not throw exceptions.
392+
393+
This overload of Cancel aggregates any exceptions thrown into an <xref:System.AggregateException>, such that one callback throwing an exception will not prevent other registered callbacks from being executed.
391394
392-
Calling this method has the same effect as calling [`Cancel(false)`](xref:System.Threading.CancellationTokenSource.Cancel(System.Boolean)).
395+
Calling this method has the same effect as calling [`Cancel(false)`](xref:System.Threading.CancellationTokenSource.Cancel(System.Boolean)).
393396
394397
## Examples
395-
The following example uses a random number generator to emulate a data collection application that reads 10 integral values from eleven different instruments. A value of zero indicates that the measurement has failed for one instrument, in which case the operation should be cancelled and no overall mean should be computed.
396398
397-
To handle the possible cancellation of the operation, the example instantiates a <xref:System.Threading.CancellationTokenSource> object that generates a cancellation token which is passed to a <xref:System.Threading.Tasks.TaskFactory> object. The <xref:System.Threading.Tasks.TaskFactory> object in turn passes the cancellation token to each of the tasks responsible for collecting readings for a particular instrument. The <xref:System.Threading.Tasks.TaskFactory.ContinueWhenAll%60%602%28System.Threading.Tasks.Task%7B%60%600%7D%5B%5D%2CSystem.Func%7BSystem.Threading.Tasks.Task%7B%60%600%7D%5B%5D%2C%60%601%7D%2CSystem.Threading.CancellationToken%29?displayProperty=nameWithType> method is called to ensure that the mean is computed only after all readings have been gathered successfully. If a task has not because it has been cancelled, the call to the <xref:System.Threading.Tasks.TaskFactory.ContinueWhenAll%2A?displayProperty=nameWithType> method throws an exception.
399+
The following example uses a random number generator to emulate a data collection application that reads 10 integral values from eleven different instruments. A value of zero indicates that the measurement has failed for one instrument, in which case the operation should be cancelled and no overall mean should be computed.
398400
399-
:::code language="csharp" source="~/snippets/csharp/System.Threading/CancellationToken/Overview/cancel1.cs" id="Snippet1":::
400-
:::code language="vb" source="~/snippets/visualbasic/System.Threading/CancellationToken/Overview/cancel1.vb" id="Snippet1":::
401+
To handle the possible cancellation of the operation, the example instantiates a <xref:System.Threading.CancellationTokenSource> object that generates a cancellation token which is passed to a <xref:System.Threading.Tasks.TaskFactory> object. The <xref:System.Threading.Tasks.TaskFactory> object in turn passes the cancellation token to each of the tasks responsible for collecting readings for a particular instrument. The <xref:System.Threading.Tasks.TaskFactory.ContinueWhenAll%60%602%28System.Threading.Tasks.Task%7B%60%600%7D%5B%5D%2CSystem.Func%7BSystem.Threading.Tasks.Task%7B%60%600%7D%5B%5D%2C%60%601%7D%2CSystem.Threading.CancellationToken%29?displayProperty=nameWithType> method is called to ensure that the mean is computed only after all readings have been gathered successfully. If a task has not because it has been cancelled, the call to the <xref:System.Threading.Tasks.TaskFactory.ContinueWhenAll%2A?displayProperty=nameWithType> method throws an exception.
402+
403+
:::code language="csharp" source="~/snippets/csharp/System.Threading/CancellationToken/Overview/cancel1.cs" id="Snippet1":::
404+
:::code language="vb" source="~/snippets/visualbasic/System.Threading/CancellationToken/Overview/cancel1.vb" id="Snippet1":::
401405
402406
]]></format>
403407
</remarks>
@@ -453,18 +457,21 @@
453457
<see langword="true" /> if exceptions should immediately propagate; otherwise, <see langword="false" />.</param>
454458
<summary>Communicates a request for cancellation, and specifies whether remaining callbacks and cancelable operations should be processed if an exception occurs.</summary>
455459
<remarks>
456-
<format type="text/markdown"><![CDATA[
457-
458-
## Remarks
459-
The associated <xref:System.Threading.CancellationToken> will be notified of the cancellation and will transition to a state where <xref:System.Threading.CancellationToken.IsCancellationRequested%2A> returns `true`.
460-
461-
Any callbacks or cancelable operations registered with the <xref:System.Threading.CancellationToken> will be executed. Callbacks will be executed synchronously in LIFO order.
460+
<format type="text/markdown"><![CDATA[
461+
462+
## Remarks
462463
463-
We recommend that cancelable operations and callbacks registered with <xref:System.Threading.CancellationToken> not throw exceptions.
464+
The associated <xref:System.Threading.CancellationToken> is notified of the cancellation and transitions to a state where <xref:System.Threading.CancellationToken.IsCancellationRequested%2A> returns `true`.
465+
466+
Any callbacks or cancelable operations registered with the <xref:System.Threading.CancellationToken> are executed, if they haven't already been executed by a previous call to <xref:System.Threading.CancellationTokenSource.Cancel>. Subsequent calls to <xref:System.Threading.CancellationTokenSource.Cancel> won't execute the same callback again unless reregistered. (Avoid multiple calls to <xref:System.Threading.CancellationTokenSource.Cancel>, because the intent of such code is often unclear.)
464467
465-
If `throwOnFirstException` is `true`, an exception will immediately propagate out of the call to <xref:System.Threading.CancellationTokenSource.Cancel%2A>, preventing the remaining callbacks and cancelable operations from being processed.
468+
Callbacks are executed synchronously in LIFO order.
469+
470+
We recommend that cancelable operations and callbacks registered with <xref:System.Threading.CancellationToken> not throw exceptions.
471+
472+
If `throwOnFirstException` is `true`, an exception will immediately propagate out of the call to <xref:System.Threading.CancellationTokenSource.Cancel%2A>, preventing the remaining callbacks and cancelable operations from being processed.
466473
467-
If `throwOnFirstException` is `false`, this overload will aggregate any exceptions thrown into an <xref:System.AggregateException>, such that one callback throwing an exception will not prevent other registered callbacks from being executed.
474+
If `throwOnFirstException` is `false`, this overload aggregates any exceptions thrown into an <xref:System.AggregateException>, such that one callback throwing an exception will not prevent other registered callbacks from being executed.
468475
469476
]]></format>
470477
</remarks>
@@ -632,8 +639,8 @@
632639
<summary>Communicates a request for cancellation asynchronously.</summary>
633640
<returns>A task that will complete after cancelable operations and callbacks registered with the associated <see cref="T:System.Threading.CancellationToken" /> have completed.</returns>
634641
<remarks>
635-
<para> The associated <see cref="T:System.Threading.CancellationToken" /> will be notified of the cancellation and will synchronously transition to a state where <see cref="P:System.Threading.CancellationToken.IsCancellationRequested" /> returns <see langword="true" />.
636-
Any callbacks or cancelable operations registered with the <see cref="T:System.Threading.CancellationToken" /> will be executed asynchronously, with the returned <see cref="T:System.Threading.Tasks.Task" /> representing their eventual completion.
642+
<para> The associated <see cref="T:System.Threading.CancellationToken" /> is notified of the cancellation and synchronously transitions to a state where <see cref="P:System.Threading.CancellationToken.IsCancellationRequested" /> returns <see langword="true" />.
643+
Any callbacks or cancelable operations registered with the <see cref="T:System.Threading.CancellationToken" /> will be executed asynchronously, with the returned <see cref="T:System.Threading.Tasks.Task" /> representing their eventual completion.
637644
</para>
638645
<para> Callbacks registered with the token should not throw exceptions.
639646
However, any such exceptions that are thrown will be aggregated into an <see cref="T:System.AggregateException" />, such that one callback throwing an exception will not prevent other registered callbacks from being executed.

0 commit comments

Comments
 (0)