1 //--------------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // File: CancellationTokenExtensions.cs
7 //--------------------------------------------------------------------------
9 using System.Collections.Concurrent.Partitioners;
10 using System.Collections.Generic;
12 namespace System.Threading
14 /// <summary>Extension methods for CancellationToken.</summary>
15 public static class CancellationTokenExtensions
17 /// <summary>Cancels a CancellationTokenSource and throws a corresponding OperationCanceledException.</summary>
18 /// <param name="source">The source to be canceled.</param>
19 public static void CancelAndThrow(this CancellationTokenSource source)
22 source.Token.ThrowIfCancellationRequested();
26 /// Creates a CancellationTokenSource that will be canceled when the specified token has cancellation requested.
28 /// <param name="token">The token.</param>
29 /// <returns>The created CancellationTokenSource.</returns>
30 public static CancellationTokenSource CreateLinkedSource(this CancellationToken token)
32 return CancellationTokenSource.CreateLinkedTokenSource(token, new CancellationToken());