All files
[pithos-ms-client] / trunk / Libraries / ParallelExtensionsExtras / Extensions / TaskFactoryExtensions / TaskFactoryExtensions_Common.cs
1 //--------------------------------------------------------------------------
2 // 
3 //  Copyright (c) Microsoft Corporation.  All rights reserved. 
4 // 
5 //  File: TaskFactoryExtensions_Common.cs
6 //
7 //--------------------------------------------------------------------------
8
9 namespace System.Threading.Tasks
10 {
11     /// <summary>Extensions for TaskFactory.</summary>
12     public static partial class TaskFactoryExtensions
13     {
14         /// <summary>Creates a generic TaskFactory from a non-generic one.</summary>
15         /// <typeparam name="TResult">Specifies the type of Task results for the Tasks created by the new TaskFactory.</typeparam>
16         /// <param name="factory">The TaskFactory to serve as a template.</param>
17         /// <returns>The created TaskFactory.</returns>
18         public static TaskFactory<TResult> ToGeneric<TResult>(this TaskFactory factory)
19         {
20             return new TaskFactory<TResult>(
21                 factory.CancellationToken, factory.CreationOptions, factory.ContinuationOptions, factory.Scheduler);
22         }
23
24         /// <summary>Creates a generic TaskFactory from a non-generic one.</summary>
25         /// <typeparam name="TResult">Specifies the type of Task results for the Tasks created by the new TaskFactory.</typeparam>
26         /// <param name="factory">The TaskFactory to serve as a template.</param>
27         /// <returns>The created TaskFactory.</returns>
28         public static TaskFactory ToNonGeneric<TResult>(this TaskFactory<TResult> factory)
29         {
30             return new TaskFactory(
31                 factory.CancellationToken, factory.CreationOptions, factory.ContinuationOptions, factory.Scheduler);
32         }
33
34         /// <summary>Gets the TaskScheduler instance that should be used to schedule tasks.</summary>
35         public static TaskScheduler GetTargetScheduler(this TaskFactory factory)
36         {
37             if (factory == null) throw new ArgumentNullException("factory");
38             return factory.Scheduler ?? TaskScheduler.Current;
39         }
40
41         /// <summary>Gets the TaskScheduler instance that should be used to schedule tasks.</summary>
42         public static TaskScheduler GetTargetScheduler<TResult>(this TaskFactory<TResult> factory)
43         {
44             if (factory == null) throw new ArgumentNullException("factory");
45             return factory.Scheduler != null ? factory.Scheduler : TaskScheduler.Current;
46         }
47
48         /// <summary>Converts TaskCreationOptions into TaskContinuationOptions.</summary>
49         /// <param name="creationOptions"></param>
50         /// <returns></returns>
51         private static TaskContinuationOptions ContinuationOptionsFromCreationOptions(TaskCreationOptions creationOptions)
52         {
53             return (TaskContinuationOptions)
54                 ((creationOptions & TaskCreationOptions.AttachedToParent) |
55                  (creationOptions & TaskCreationOptions.PreferFairness) |
56                  (creationOptions & TaskCreationOptions.LongRunning));
57         }
58     }
59 }