5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 using System.Threading;
32 namespace System.Threading.Tasks
35 public class TaskFactory
37 TaskScheduler scheduler;
38 TaskCreationOptions creationOptions;
39 TaskContinuationOptions continuationOptions;
40 CancellationToken cancellationToken;
44 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
48 public TaskFactory (CancellationToken cancellationToken)
49 : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
53 public TaskFactory (TaskScheduler scheduler)
54 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
58 public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
59 : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
63 public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
64 TaskScheduler scheduler)
66 this.cancellationToken = cancellationToken;
67 this.scheduler = scheduler;
68 this.creationOptions = creationOptions;
69 this.continuationOptions = continuationOptions;
73 #region StartNew for Task
74 public Task StartNew (Action action)
76 return StartNew (action, cancellationToken, creationOptions, scheduler);
79 public Task StartNew (Action action, CancellationToken cancellationToken)
81 return StartNew (action, cancellationToken, creationOptions, scheduler);
84 public Task StartNew (Action action, TaskCreationOptions creationOptions)
86 return StartNew (action, cancellationToken, creationOptions, scheduler);
89 public Task StartNew (Action<object> action, object state)
91 return StartNew (action, state, cancellationToken, creationOptions, scheduler);
94 public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken)
96 return StartNew (action, state, cancellationToken, creationOptions, scheduler);
99 public Task StartNew (Action<object> action, object state, TaskCreationOptions creationOptions)
101 return StartNew (action, state, cancellationToken, creationOptions, scheduler);
104 public Task StartNew (Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
106 Task t = new Task (action, cancellationToken, creationOptions);
112 public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
113 TaskScheduler scheduler)
115 Task t = new Task (action, state, cancellationToken, creationOptions);
122 #region StartNew for Task<TResult>
123 public Task<TResult> StartNew<TResult> (Func<TResult> function)
125 return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
128 public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
130 return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
134 public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
136 return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
139 public Task<TResult> StartNew<TResult> (Func<TResult> function,
140 CancellationToken cancellationToken,
141 TaskCreationOptions creationOptions,
142 TaskScheduler scheduler)
144 return StartNew<TResult> ((o) => function (), null, cancellationToken, creationOptions, scheduler);
147 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
149 return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
152 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
154 return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
157 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
159 return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
162 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
163 CancellationToken cancellationToken,
164 TaskCreationOptions creationOptions,
165 TaskScheduler scheduler)
167 Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
176 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
178 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
181 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
183 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
186 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
188 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
191 public Task ContinueWhenAny (Task[] tasks,
192 Action<Task> continuationAction,
193 CancellationToken cancellationToken,
194 TaskContinuationOptions continuationOptions,
195 TaskScheduler scheduler)
197 var ourTasks = (Task[])tasks.Clone ();
198 AtomicBoolean trigger = new AtomicBoolean ();
199 Task commonContinuation = new Task (null);
201 foreach (Task t in ourTasks) {
202 Task cont = new Task ((o) => continuationAction ((Task)o), t, cancellationToken, creationOptions, t);
203 t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
204 cont.ContinueWithCore (commonContinuation, TaskContinuationOptions.None, scheduler);
207 return commonContinuation;
210 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
211 Action<Task<TAntecedentResult>> continuationAction)
213 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
216 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
217 Action<Task<TAntecedentResult>> continuationAction,
218 CancellationToken cancellationToken)
220 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
223 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
224 Action<Task<TAntecedentResult>> continuationAction,
225 TaskContinuationOptions continuationOptions)
227 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
230 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
231 Action<Task<TAntecedentResult>> continuationAction,
232 CancellationToken cancellationToken,
233 TaskContinuationOptions continuationOptions,
234 TaskScheduler scheduler)
236 return ContinueWhenAny ((Task[]) tasks,
237 (o) => continuationAction ((Task<TAntecedentResult>)o),
238 cancellationToken, continuationOptions, scheduler);
241 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
243 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
246 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
247 Func<Task, TResult> continuationFunction,
248 CancellationToken cancellationToken)
250 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
253 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
254 Func<Task, TResult> continuationFunction,
255 TaskContinuationOptions continuationOptions)
257 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
260 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
261 Func<Task, TResult> continuationFunction,
262 CancellationToken cancellationToken,
263 TaskContinuationOptions continuationOptions,
264 TaskScheduler scheduler)
266 var ourTasks = (Task[])tasks.Clone ();
267 AtomicBoolean trigger = new AtomicBoolean ();
268 TaskCompletionSource<TResult> source = new TaskCompletionSource<TResult> ();
270 foreach (Task t in ourTasks) {
271 Task cont = new Task ((o) => source.SetResult (continuationFunction ((Task)o)), t, cancellationToken, creationOptions, t);
272 t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
278 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
279 Func<Task<TAntecedentResult>, TResult> continuationFunction)
281 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
284 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
285 Func<Task<TAntecedentResult>, TResult> continuationFunction,
286 CancellationToken cancellationToken)
288 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
291 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
292 Func<Task<TAntecedentResult>, TResult> continuationFunction,
293 TaskContinuationOptions continuationOptions)
295 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
298 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
299 Func<Task<TAntecedentResult>, TResult> continuationFunction,
300 CancellationToken cancellationToken,
301 TaskContinuationOptions continuationOptions,
302 TaskScheduler scheduler)
304 return ContinueWhenAny<TResult> ((Task[])tasks,
305 (t) => continuationFunction((Task<TAntecedentResult>)t),
311 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
313 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
316 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
318 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
321 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
322 TaskContinuationOptions continuationOptions)
324 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
327 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
328 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
330 var ourTasks = (Task[])tasks.Clone ();
331 CountdownEvent evt = new CountdownEvent (ourTasks.Length);
332 Task cont = new Task ((o) => continuationAction ((Task[])o), ourTasks, cancellationToken, creationOptions);
334 foreach (Task t in ourTasks)
335 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
340 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
341 Action<Task<TAntecedentResult>[]> continuationAction)
343 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
346 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
347 Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
349 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
352 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
353 TaskContinuationOptions continuationOptions)
355 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
358 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
359 Action<Task<TAntecedentResult>[]> continuationAction,
360 CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
361 TaskScheduler scheduler)
363 return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
364 continuationOptions, scheduler);
367 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
369 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
372 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
373 TaskContinuationOptions continuationOptions)
375 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
378 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
379 CancellationToken cancellationToken)
381 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
384 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
385 CancellationToken cancellationToken,
386 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
388 var ourTasks = (Task[])tasks.Clone ();
389 CountdownEvent evt = new CountdownEvent (ourTasks.Length);
390 Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), ourTasks, cancellationToken, creationOptions);
392 foreach (Task t in ourTasks)
393 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
398 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
399 Func<Task<TAntecedentResult>[], TResult> continuationFunction)
401 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
404 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
405 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
406 TaskContinuationOptions continuationOptions)
408 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
411 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
412 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
413 CancellationToken cancellationToken)
415 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
418 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
419 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
420 CancellationToken cancellationToken,
421 TaskContinuationOptions continuationOptions,
422 TaskScheduler scheduler)
424 return ContinueWhenAll<TResult> ((Task[]) tasks,
425 (o) => continuationFunction (tasks),
427 continuationOptions, scheduler);
433 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
435 return FromAsync (asyncResult, endMethod, creationOptions, scheduler);
438 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
439 TaskCreationOptions creationOptions)
441 return FromAsync (asyncResult, endMethod, creationOptions, scheduler);
444 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
445 TaskCreationOptions creationOptions, TaskScheduler scheduler)
447 return FromAsync<object> (asyncResult, (ar) => { endMethod (ar); return null; }, creationOptions, scheduler);
450 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
452 return FromAsync<TResult> (asyncResult, endMethod, creationOptions, scheduler);
455 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
456 TaskCreationOptions creationOptions)
458 return FromAsync<TResult> (asyncResult, endMethod, creationOptions, scheduler);
461 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
462 TaskCreationOptions creationOptions, TaskScheduler scheduler)
464 var completionSource = new TaskCompletionSource<TResult> ();
466 ThreadPool.RegisterWaitForSingleObject (asyncResult.AsyncWaitHandle,
469 completionSource.SetResult (endMethod (asyncResult));
470 } catch (Exception e) {
471 completionSource.SetException (e);
478 return completionSource.Task;
481 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
482 Action<IAsyncResult> endMethod,
485 return FromAsync (beginMethod, endMethod, state, creationOptions);
488 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
489 Action<IAsyncResult> endMethod,
490 object state, TaskCreationOptions creationOptions)
492 return FromAsync (beginMethod, (ar) => { endMethod (ar); return (object)null; }, state, creationOptions);
495 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
496 TArg1 arg1, object state)
498 return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
501 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
502 TArg1 arg1, object state, TaskCreationOptions creationOptions)
504 return FromAsync (beginMethod, (ar) => { endMethod (ar); return (object)null; }, arg1, state, creationOptions);
507 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
508 Action<IAsyncResult> endMethod,
509 TArg1 arg1, TArg2 arg2, object state)
511 return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
514 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
515 Action<IAsyncResult> endMethod,
516 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
518 return FromAsync (beginMethod, (ar) => { endMethod (ar); return (object)null; }, arg1, arg2, state, creationOptions);
521 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
522 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
524 return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
527 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
528 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
530 return FromAsync (beginMethod, (ar) => { endMethod (ar); return (object)null; }, arg1, arg2, arg3, state, creationOptions);
533 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
534 Func<IAsyncResult, TResult> endMethod,
537 return FromAsync (beginMethod, endMethod, state, creationOptions);
540 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
541 Func<IAsyncResult, TResult> endMethod,
542 object state, TaskCreationOptions creationOptions)
544 var completionSource = new TaskCompletionSource<TResult> (creationOptions);
545 beginMethod ((ar) => {
547 completionSource.SetResult (endMethod (ar));
548 } catch (Exception e) {
549 completionSource.SetException (e);
553 return completionSource.Task;
556 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
557 Func<IAsyncResult, TResult> endMethod,
558 TArg1 arg1, object state)
560 return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
563 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
564 Func<IAsyncResult, TResult> endMethod,
565 TArg1 arg1, object state, TaskCreationOptions creationOptions)
567 var completionSource = new TaskCompletionSource<TResult> (creationOptions);
568 beginMethod (arg1, (ar) => {
570 completionSource.SetResult (endMethod (ar));
571 } catch (Exception e) {
572 completionSource.SetException (e);
576 return completionSource.Task;
579 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
580 Func<IAsyncResult, TResult> endMethod,
581 TArg1 arg1, TArg2 arg2, object state)
583 return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
586 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
587 Func<IAsyncResult, TResult> endMethod,
588 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
590 var completionSource = new TaskCompletionSource<TResult> (creationOptions);
591 beginMethod (arg1, arg2, (ar) => {
593 completionSource.SetResult (endMethod (ar));
594 } catch (Exception e) {
595 completionSource.SetException (e);
599 return completionSource.Task;
602 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
603 Func<IAsyncResult, TResult> endMethod,
604 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
606 return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
609 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
610 Func<IAsyncResult, TResult> endMethod,
611 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
612 TaskCreationOptions creationOptions)
614 var completionSource = new TaskCompletionSource<TResult> (creationOptions);
615 beginMethod (arg1, arg2, arg3, (ar) => {
617 completionSource.SetResult (endMethod (ar));
618 } catch (Exception e) {
619 completionSource.SetException (e);
623 return completionSource.Task;
627 public TaskScheduler Scheduler {
633 public TaskContinuationOptions ContinuationOptions {
635 return continuationOptions;
639 public TaskCreationOptions CreationOptions {
641 return creationOptions;
645 public CancellationToken CancellationToken {
647 return cancellationToken;
652 public class TaskFactory<TResult>
654 TaskScheduler scheduler;
655 TaskCreationOptions creationOptions;
656 TaskContinuationOptions continuationOptions;
657 CancellationToken cancellationToken;
662 public TaskFactory ()
663 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
667 public TaskFactory (TaskScheduler scheduler)
668 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
672 public TaskFactory (CancellationToken cancellationToken)
673 : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
677 public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
678 : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
682 public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
683 TaskScheduler scheduler)
685 this.cancellationToken = cancellationToken;
686 this.scheduler = scheduler;
687 this.creationOptions = creationOptions;
688 this.continuationOptions = continuationOptions;
690 this.parent = new TaskFactory (cancellationToken, creationOptions, continuationOptions, scheduler);
695 #region StartNew for Task<TResult>
696 public Task<TResult> StartNew (Func<TResult> function)
698 return StartNew (function, cancellationToken, creationOptions, scheduler);
701 public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions creationOptions)
703 return StartNew (function, cancellationToken, creationOptions, scheduler);
706 public Task<TResult> StartNew (Func<TResult> function, CancellationToken cancellationToken)
708 return StartNew (function, cancellationToken, creationOptions, scheduler);
711 public Task<TResult> StartNew (Func<TResult> function,
712 CancellationToken cancellationToken,
713 TaskCreationOptions creationOptions,
714 TaskScheduler scheduler)
716 return StartNew ((o) => function (), null, cancellationToken, creationOptions, scheduler);
719 public Task<TResult> StartNew (Func<object, TResult> function, object state)
721 return StartNew (function, state, cancellationToken, creationOptions, scheduler);
724 public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
726 return StartNew (function, state, cancellationToken, creationOptions, scheduler);
729 public Task<TResult> StartNew (Func<object, TResult> function, object state, CancellationToken cancellationToken)
731 return StartNew (function, state, cancellationToken, creationOptions, scheduler);
734 public Task<TResult> StartNew (Func<object, TResult> function, object state,
735 CancellationToken cancellationToken,
736 TaskCreationOptions creationOptions,
737 TaskScheduler scheduler)
739 return parent.StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
745 public Task<TResult> ContinueWhenAny (Task[] tasks,
746 Func<Task, TResult> continuationFunction)
748 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
751 public Task<TResult> ContinueWhenAny (Task[] tasks,
752 Func<Task, TResult> continuationFunction,
753 CancellationToken cancellationToken)
755 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
758 public Task<TResult> ContinueWhenAny (Task[] tasks,
759 Func<Task, TResult> continuationFunction,
760 TaskContinuationOptions continuationOptions)
762 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
765 public Task<TResult> ContinueWhenAny (Task[] tasks,
766 Func<Task, TResult> continuationFunction,
767 CancellationToken cancellationToken,
768 TaskContinuationOptions continuationOptions,
769 TaskScheduler scheduler)
771 return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
774 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
775 Func<Task<TAntecedentResult>, TResult> continuationFunction)
777 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
780 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
781 Func<Task<TAntecedentResult>, TResult> continuationFunction,
782 CancellationToken cancellationToken)
784 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
787 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
788 Func<Task<TAntecedentResult>, TResult> continuationFunction,
789 TaskContinuationOptions continuationOptions)
791 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
794 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
795 Func<Task<TAntecedentResult>, TResult> continuationFunction,
796 CancellationToken cancellationToken,
797 TaskContinuationOptions continuationOptions,
798 TaskScheduler scheduler)
800 return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
803 public Task<TResult> ContinueWhenAll (Task[] tasks,
804 Func<Task[], TResult> continuationFunction)
806 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
809 public Task<TResult> ContinueWhenAll (Task[] tasks,
810 Func<Task[], TResult> continuationFunction,
811 TaskContinuationOptions continuationOptions)
813 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
816 public Task<TResult> ContinueWhenAll (Task[] tasks,
817 Func<Task[], TResult> continuationFunction,
818 CancellationToken cancellationToken)
820 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
823 public Task<TResult> ContinueWhenAll (Task[] tasks,
824 Func<Task[], TResult> continuationFunction,
825 CancellationToken cancellationToken,
826 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
828 return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
831 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
832 Func<Task<TAntecedentResult>[], TResult> continuationFunction)
834 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
837 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
838 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
839 TaskContinuationOptions continuationOptions)
841 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
844 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
845 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
846 CancellationToken cancellationToken)
848 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
851 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
852 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
853 CancellationToken cancellationToken,
854 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
856 return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
862 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
864 [MonoLimitation(errorMsg)]
865 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
867 return FromAsync (asyncResult, endMethod, creationOptions);
870 [MonoLimitation(errorMsg)]
871 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
872 TaskCreationOptions creationOptions)
874 return FromAsync (asyncResult, endMethod, creationOptions);
877 [MonoLimitation(errorMsg)]
878 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
879 TaskCreationOptions creationOptions, TaskScheduler scheduler)
881 throw new NotSupportedException (errorMsg);
884 [MonoLimitation(errorMsg)]
885 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
886 Func<IAsyncResult, TResult> endMethod,
889 throw new NotSupportedException (errorMsg);
892 [MonoLimitation(errorMsg)]
893 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
894 Func<IAsyncResult, TResult> endMethod,
895 object state, TaskCreationOptions creationOptions)
897 throw new NotSupportedException (errorMsg);
900 [MonoLimitation(errorMsg)]
901 public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
902 Func<IAsyncResult, TResult> endMethod,
903 TArg1 arg1, object state)
905 throw new NotSupportedException (errorMsg);
908 [MonoLimitation(errorMsg)]
909 public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
910 Func<IAsyncResult, TResult> endMethod,
911 TArg1 arg1, object state, TaskCreationOptions creationOptions)
913 throw new NotSupportedException (errorMsg);
916 [MonoLimitation(errorMsg)]
917 public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
918 Func<IAsyncResult, TResult> endMethod,
919 TArg1 arg1, TArg2 arg2, object state)
921 throw new NotSupportedException (errorMsg);
924 [MonoLimitation(errorMsg)]
925 public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
926 Func<IAsyncResult, TResult> endMethod,
927 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
929 throw new NotSupportedException (errorMsg);
932 [MonoLimitation(errorMsg)]
933 public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
934 Func<IAsyncResult, TResult> endMethod,
935 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
937 throw new NotSupportedException (errorMsg);
940 [MonoLimitation(errorMsg)]
941 public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
942 Func<IAsyncResult, TResult> endMethod,
943 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
944 TaskCreationOptions creationOptions)
946 throw new NotSupportedException (errorMsg);
950 public TaskScheduler Scheduler {
956 public TaskContinuationOptions ContinuationOptions {
958 return continuationOptions;
962 public TaskCreationOptions CreationOptions {
964 return creationOptions;
968 public CancellationToken CancellationToken {
970 return cancellationToken;