4 // Copyright (c) 2008 Jérémie "Garuma" Laval
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 namespace System.Threading.Tasks
31 [System.Diagnostics.DebuggerDisplay ("Id = {Id}, Status = {Status}, Method = {DebuggerDisplayMethodDescription}, Result = {DebuggerDisplayResultDescription}")]
32 [System.Diagnostics.DebuggerTypeProxy ("System.Threading.Tasks.SystemThreadingTasks_FutureDebugView`1")]
33 public class Task<TResult>: Task
36 static TaskFactory<TResult> factory = new TaskFactory<TResult> ();
37 static readonly Action<object> emptyAction = delegate {};
39 Func<object, TResult> function;
42 [System.Diagnostics.DebuggerBrowsable (System.Diagnostics.DebuggerBrowsableState.Never)]
43 public TResult Result {
47 else if (Exception != null)
56 public static new TaskFactory<TResult> Factory {
62 public Task (Func<TResult> function) : this (function, TaskCreationOptions.None)
67 public Task (Func<TResult> function, CancellationToken cancellationToken)
68 : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, TaskCreationOptions.None)
73 public Task (Func<TResult> function, TaskCreationOptions creationOptions)
74 : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, CancellationToken.None, creationOptions)
79 public Task (Func<TResult> function, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
80 : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, creationOptions)
85 public Task (Func<object, TResult> function, object state) : this (function, state, TaskCreationOptions.None)
90 public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken)
91 : this (function, state, cancellationToken, TaskCreationOptions.None)
96 public Task (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
97 : this (function, state, CancellationToken.None, creationOptions)
102 public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
103 : base (emptyAction, state, cancellationToken, creationOptions)
105 this.function = function;
109 internal Task (Func<object, TResult> function,
111 CancellationToken cancellationToken,
112 TaskCreationOptions creationOptions,
114 : base (null, state, cancellationToken, creationOptions, parent)
116 this.function = function;
120 internal override void InnerInvoke ()
122 if (function != null)
123 value = function (state);
129 public Task ContinueWith (Action<Task<TResult>> continuationAction)
131 return ContinueWith (continuationAction, TaskContinuationOptions.None);
134 public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskContinuationOptions continuationOptions)
136 return ContinueWith (continuationAction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
139 public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken)
141 return ContinueWith (continuationAction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
144 public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskScheduler scheduler)
146 return ContinueWith (continuationAction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
149 public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken,
150 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
152 if (continuationAction == null)
153 throw new ArgumentNullException ("continuationFunction");
154 if (scheduler == null)
155 throw new ArgumentNullException ("scheduler");
157 Task t = new Task ((o) => continuationAction ((Task<TResult>)o),
160 GetCreationOptions (continuationOptions),
162 ContinueWithCore (t, continuationOptions, scheduler);
167 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction)
169 return ContinueWith<TNewResult> (continuationFunction, TaskContinuationOptions.None);
172 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, CancellationToken cancellationToken)
174 return ContinueWith<TNewResult> (continuationFunction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
177 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskContinuationOptions continuationOptions)
179 return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
182 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskScheduler scheduler)
184 return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
187 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction,
188 CancellationToken cancellationToken,
189 TaskContinuationOptions continuationOptions,
190 TaskScheduler scheduler)
192 Task<TNewResult> t = new Task<TNewResult> ((o) => continuationFunction ((Task<TResult>)o),
195 GetCreationOptions (continuationOptions),
197 ContinueWithCore (t, continuationOptions, scheduler);