All files
[pithos-ms-client] / trunk / Libraries / ParallelExtensionsExtras / Extensions / CancellationTokenExtensions.cs
1 //--------------------------------------------------------------------------
2 // 
3 //  Copyright (c) Microsoft Corporation.  All rights reserved. 
4 // 
5 //  File: CancellationTokenExtensions.cs
6 //
7 //--------------------------------------------------------------------------
8
9 using System.Collections.Concurrent.Partitioners;
10 using System.Collections.Generic;
11
12 namespace System.Threading
13 {
14     /// <summary>Extension methods for CancellationToken.</summary>
15     public static class CancellationTokenExtensions
16     {
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)
20         {
21             source.Cancel();
22             source.Token.ThrowIfCancellationRequested();
23         }
24
25         /// <summary>
26         /// Creates a CancellationTokenSource that will be canceled when the specified token has cancellation requested.
27         /// </summary>
28         /// <param name="token">The token.</param>
29         /// <returns>The created CancellationTokenSource.</returns>
30         public static CancellationTokenSource CreateLinkedSource(this CancellationToken token)
31         {
32             return CancellationTokenSource.CreateLinkedTokenSource(token, new CancellationToken());
33         }
34     }
35 }