-
Notifications
You must be signed in to change notification settings - Fork 5k
/
CancellationTokenExtensions.cs
26 lines (24 loc) · 1.14 KB
/
CancellationTokenExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
namespace System.Threading
{
/// <summary>Extension methods for CancellationToken.</summary>
public static class CancellationTokenExtensions
{
/// <summary>Cancels a CancellationTokenSource and throws a corresponding OperationCanceledException.</summary>
/// <param name="source">The source to be canceled.</param>
public static void CancelAndThrow(this CancellationTokenSource source)
{
source.Cancel();
source.Token.ThrowIfCancellationRequested();
}
/// <summary>
/// Creates a CancellationTokenSource that will be canceled when the specified token has cancellation requested.
/// </summary>
/// <param name="token">The token.</param>
/// <returns>The created CancellationTokenSource.</returns>
public static CancellationTokenSource CreateLinkedSource(this CancellationToken token) => CancellationTokenSource.CreateLinkedTokenSource(token, new CancellationToken());
}
}