| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Register Callbacks for Cancellation Requests |
03/30/2017 |
.net |
dotnet-standard |
article |
|
8838dd75-18ed-4b8b-b322-cd4531faac64 |
9 |
rpetrusha |
ronpet |
wpickett |
How to: Register Callbacks for Cancellation Requests
The following example shows how to register a delegate that will be invoked when a xref:System.Threading.CancellationToken.IsCancellationRequested%2A property becomes true due to a call to xref:System.Threading.CancellationTokenSource.Cancel%2A on the object that created the token. Use this technique for cancelling asynchronous operations that do not natively support the unified cancellation framework, and for unblocking methods that might be waiting for an asynchronous operation to finish.
[!NOTE] When "Just My Code" is enabled, Visual Studio in some cases will break on the line that throws the exception and display an error message that says "exception not handled by user code." This error is benign. You can press F5 to continue from it, and see the exception-handling behavior that is demonstrated in the examples below. To prevent Visual Studio from breaking on the first error, just uncheck the "Just My Code" checkbox under Tools, Options, Debugging, General.
Example
In the following example, the xref:System.Net.WebClient.CancelAsync%2A method is registered as the method to be invoked when cancellation is requested through the cancellation token.
[!code-csharpConceptual.Cancellation.Callback#1] [!code-vbConceptual.Cancellation.Callback#1]
If cancellation has already been requested when the callback is registered, the callback is still guaranteed to be called. In this particular case, the xref:System.Net.WebClient.CancelAsync%2A method will do nothing if no asynchronous operation is in progress, so it is always safe to call the method.