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 return StartNew ((o) => action (), null, cancellationToken, creationOptions, scheduler);
109 public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
110 TaskScheduler scheduler)
112 Task t = new Task (action, state, cancellationToken, creationOptions);
119 #region StartNew for Task<TResult>
120 public Task<TResult> StartNew<TResult> (Func<TResult> function)
122 return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
125 public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
127 return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
131 public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
133 return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
136 public Task<TResult> StartNew<TResult> (Func<TResult> function,
137 CancellationToken cancellationToken,
138 TaskCreationOptions creationOptions,
139 TaskScheduler scheduler)
141 return StartNew<TResult> ((o) => function (), null, cancellationToken, creationOptions, scheduler);
144 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
146 return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
149 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
151 return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
154 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
156 return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
159 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
160 CancellationToken cancellationToken,
161 TaskCreationOptions creationOptions,
162 TaskScheduler scheduler)
164 Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
173 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
175 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
178 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
180 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
183 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
185 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
188 public Task ContinueWhenAny (Task[] tasks,
189 Action<Task> continuationAction,
190 CancellationToken cancellationToken,
191 TaskContinuationOptions continuationOptions,
192 TaskScheduler scheduler)
194 var ourTasks = (Task[])tasks.Clone ();
195 AtomicBoolean trigger = new AtomicBoolean ();
196 Task commonContinuation = new Task (null);
198 foreach (Task t in ourTasks) {
199 Task cont = new Task ((o) => continuationAction ((Task)o), t, cancellationToken, creationOptions, t);
200 t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
201 cont.ContinueWithCore (commonContinuation, TaskContinuationOptions.None, scheduler);
204 return commonContinuation;
207 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
208 Action<Task<TAntecedentResult>> continuationAction)
210 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
213 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
214 Action<Task<TAntecedentResult>> continuationAction,
215 CancellationToken cancellationToken)
217 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
220 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
221 Action<Task<TAntecedentResult>> continuationAction,
222 TaskContinuationOptions continuationOptions)
224 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
227 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
228 Action<Task<TAntecedentResult>> continuationAction,
229 CancellationToken cancellationToken,
230 TaskContinuationOptions continuationOptions,
231 TaskScheduler scheduler)
233 return ContinueWhenAny ((Task[]) tasks,
234 (o) => continuationAction ((Task<TAntecedentResult>)o),
235 cancellationToken, continuationOptions, scheduler);
238 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
240 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
243 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
244 Func<Task, TResult> continuationFunction,
245 CancellationToken cancellationToken)
247 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
250 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
251 Func<Task, TResult> continuationFunction,
252 TaskContinuationOptions continuationOptions)
254 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
257 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
258 Func<Task, TResult> continuationFunction,
259 CancellationToken cancellationToken,
260 TaskContinuationOptions continuationOptions,
261 TaskScheduler scheduler)
263 var ourTasks = (Task[])tasks.Clone ();
264 AtomicBoolean trigger = new AtomicBoolean ();
265 TaskCompletionSource<TResult> source = new TaskCompletionSource<TResult> ();
267 foreach (Task t in ourTasks) {
268 Task cont = new Task ((o) => source.SetResult (continuationFunction ((Task)o)), t, cancellationToken, creationOptions, t);
269 t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
275 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
276 Func<Task<TAntecedentResult>, TResult> continuationFunction)
278 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
281 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
282 Func<Task<TAntecedentResult>, TResult> continuationFunction,
283 CancellationToken cancellationToken)
285 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
288 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
289 Func<Task<TAntecedentResult>, TResult> continuationFunction,
290 TaskContinuationOptions continuationOptions)
292 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
295 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
296 Func<Task<TAntecedentResult>, TResult> continuationFunction,
297 CancellationToken cancellationToken,
298 TaskContinuationOptions continuationOptions,
299 TaskScheduler scheduler)
301 return ContinueWhenAny<TResult> ((Task[])tasks,
302 (t) => continuationFunction((Task<TAntecedentResult>)t),
308 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
310 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
313 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
315 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
318 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
319 TaskContinuationOptions continuationOptions)
321 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
324 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
325 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
327 var ourTasks = (Task[])tasks.Clone ();
328 CountdownEvent evt = new CountdownEvent (ourTasks.Length);
329 Task cont = new Task ((o) => continuationAction ((Task[])o), ourTasks, cancellationToken, creationOptions);
331 foreach (Task t in ourTasks)
332 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
337 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
338 Action<Task<TAntecedentResult>[]> continuationAction)
340 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
343 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
344 Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
346 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
349 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
350 TaskContinuationOptions continuationOptions)
352 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
355 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
356 Action<Task<TAntecedentResult>[]> continuationAction,
357 CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
358 TaskScheduler scheduler)
360 return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
361 continuationOptions, scheduler);
364 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
366 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
369 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
370 TaskContinuationOptions continuationOptions)
372 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
375 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
376 CancellationToken cancellationToken)
378 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
381 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
382 CancellationToken cancellationToken,
383 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
385 var ourTasks = (Task[])tasks.Clone ();
386 CountdownEvent evt = new CountdownEvent (ourTasks.Length);
387 Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), ourTasks, cancellationToken, creationOptions);
389 foreach (Task t in ourTasks)
390 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
395 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
396 Func<Task<TAntecedentResult>[], TResult> continuationFunction)
398 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
401 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
402 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
403 TaskContinuationOptions continuationOptions)
405 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
408 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
409 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
410 CancellationToken cancellationToken)
412 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
415 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
416 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
417 CancellationToken cancellationToken,
418 TaskContinuationOptions continuationOptions,
419 TaskScheduler scheduler)
421 return ContinueWhenAll<TResult> ((Task[]) tasks,
422 (o) => continuationFunction (tasks),
424 continuationOptions, scheduler);
430 // For these methods to work we first have to convert the ThreadPool to use Tasks as it
431 // is doing in 4.0, then all that is remaining is to identify the Task on which is
432 // run the async operation (probably with some additional state in a IAsyncResult subclass)
433 // and call its ContinueWith method accordingly
435 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
437 [MonoLimitation(errorMsg)]
438 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
440 return FromAsync (asyncResult, endMethod, creationOptions);
443 [MonoLimitation(errorMsg)]
444 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
445 TaskCreationOptions creationOptions)
447 return FromAsync (asyncResult, endMethod, creationOptions);
450 [MonoLimitation(errorMsg)]
451 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
452 TaskCreationOptions creationOptions, TaskScheduler scheduler)
454 throw new NotSupportedException (errorMsg);
457 [MonoLimitation(errorMsg)]
458 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
460 return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
463 [MonoLimitation(errorMsg)]
464 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
465 TaskCreationOptions creationOptions)
467 return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
470 [MonoLimitation(errorMsg)]
471 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
472 TaskCreationOptions creationOptions, TaskScheduler scheduler)
474 throw new NotSupportedException (errorMsg);
478 [MonoLimitation(errorMsg)]
479 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
482 return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
485 [MonoLimitation(errorMsg)]
486 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
487 object state, TaskCreationOptions creationOptions)
489 return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
492 [MonoLimitation(errorMsg)]
493 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
494 TArg1 arg1, object state)
496 throw new NotSupportedException (errorMsg);
499 [MonoLimitation(errorMsg)]
500 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
501 TArg1 arg1, object state, TaskCreationOptions creationOptions)
503 throw new NotSupportedException (errorMsg);
506 [MonoLimitation(errorMsg)]
507 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
508 TArg1 arg1, TArg2 arg2, object state)
510 throw new NotSupportedException (errorMsg);
513 [MonoLimitation(errorMsg)]
514 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
515 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
517 throw new NotSupportedException (errorMsg);
520 [MonoLimitation(errorMsg)]
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 throw new NotSupportedException (errorMsg);
527 [MonoLimitation(errorMsg)]
528 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
529 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
531 throw new NotSupportedException (errorMsg);
534 [MonoLimitation(errorMsg)]
535 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
536 Func<IAsyncResult, TResult> endMethod,
539 throw new NotSupportedException (errorMsg);
542 [MonoLimitation(errorMsg)]
543 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
544 Func<IAsyncResult, TResult> endMethod,
545 object state, TaskCreationOptions creationOptions)
547 throw new NotSupportedException (errorMsg);
550 [MonoLimitation(errorMsg)]
551 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
552 Func<IAsyncResult, TResult> endMethod,
553 TArg1 arg1, object state)
555 throw new NotSupportedException (errorMsg);
558 [MonoLimitation(errorMsg)]
559 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
560 Func<IAsyncResult, TResult> endMethod,
561 TArg1 arg1, object state, TaskCreationOptions creationOptions)
563 throw new NotSupportedException (errorMsg);
566 [MonoLimitation(errorMsg)]
567 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
568 Func<IAsyncResult, TResult> endMethod,
569 TArg1 arg1, TArg2 arg2, object state)
571 throw new NotSupportedException (errorMsg);
574 [MonoLimitation(errorMsg)]
575 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
576 Func<IAsyncResult, TResult> endMethod,
577 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
579 throw new NotSupportedException (errorMsg);
582 [MonoLimitation(errorMsg)]
583 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
584 Func<IAsyncResult, TResult> endMethod,
585 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
587 throw new NotSupportedException (errorMsg);
590 [MonoLimitation(errorMsg)]
591 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
592 Func<IAsyncResult, TResult> endMethod,
593 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
594 TaskCreationOptions creationOptions)
596 throw new NotSupportedException (errorMsg);
600 public TaskScheduler Scheduler {
606 public TaskContinuationOptions ContinuationOptions {
608 return continuationOptions;
612 public TaskCreationOptions CreationOptions {
614 return creationOptions;
618 public CancellationToken CancellationToken {
620 return cancellationToken;
625 public class TaskFactory<TResult>
627 TaskScheduler scheduler;
628 TaskCreationOptions creationOptions;
629 TaskContinuationOptions continuationOptions;
630 CancellationToken cancellationToken;
635 public TaskFactory ()
636 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
640 public TaskFactory (TaskScheduler scheduler)
641 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
645 public TaskFactory (CancellationToken cancellationToken)
646 : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
650 public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
651 : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
655 public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
656 TaskScheduler scheduler)
658 this.cancellationToken = cancellationToken;
659 this.scheduler = scheduler;
660 this.creationOptions = creationOptions;
661 this.continuationOptions = continuationOptions;
663 this.parent = new TaskFactory (cancellationToken, creationOptions, continuationOptions, scheduler);
668 #region StartNew for Task<TResult>
669 public Task<TResult> StartNew (Func<TResult> function)
671 return StartNew (function, cancellationToken, creationOptions, scheduler);
674 public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions creationOptions)
676 return StartNew (function, cancellationToken, creationOptions, scheduler);
679 public Task<TResult> StartNew (Func<TResult> function, CancellationToken cancellationToken)
681 return StartNew (function, cancellationToken, creationOptions, scheduler);
684 public Task<TResult> StartNew (Func<TResult> function,
685 CancellationToken cancellationToken,
686 TaskCreationOptions creationOptions,
687 TaskScheduler scheduler)
689 return StartNew ((o) => function (), null, cancellationToken, creationOptions, scheduler);
692 public Task<TResult> StartNew (Func<object, TResult> function, object state)
694 return StartNew (function, state, cancellationToken, creationOptions, scheduler);
697 public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
699 return StartNew (function, state, cancellationToken, creationOptions, scheduler);
702 public Task<TResult> StartNew (Func<object, TResult> function, object state, CancellationToken cancellationToken)
704 return StartNew (function, state, cancellationToken, creationOptions, scheduler);
707 public Task<TResult> StartNew (Func<object, TResult> function, object state,
708 CancellationToken cancellationToken,
709 TaskCreationOptions creationOptions,
710 TaskScheduler scheduler)
712 return parent.StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
718 public Task<TResult> ContinueWhenAny (Task[] tasks,
719 Func<Task, TResult> continuationFunction)
721 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
724 public Task<TResult> ContinueWhenAny (Task[] tasks,
725 Func<Task, TResult> continuationFunction,
726 CancellationToken cancellationToken)
728 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
731 public Task<TResult> ContinueWhenAny (Task[] tasks,
732 Func<Task, TResult> continuationFunction,
733 TaskContinuationOptions continuationOptions)
735 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
738 public Task<TResult> ContinueWhenAny (Task[] tasks,
739 Func<Task, TResult> continuationFunction,
740 CancellationToken cancellationToken,
741 TaskContinuationOptions continuationOptions,
742 TaskScheduler scheduler)
744 return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
747 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
748 Func<Task<TAntecedentResult>, TResult> continuationFunction)
750 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
753 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
754 Func<Task<TAntecedentResult>, TResult> continuationFunction,
755 CancellationToken cancellationToken)
757 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
760 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
761 Func<Task<TAntecedentResult>, TResult> continuationFunction,
762 TaskContinuationOptions continuationOptions)
764 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
767 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
768 Func<Task<TAntecedentResult>, TResult> continuationFunction,
769 CancellationToken cancellationToken,
770 TaskContinuationOptions continuationOptions,
771 TaskScheduler scheduler)
773 return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
776 public Task<TResult> ContinueWhenAll (Task[] tasks,
777 Func<Task[], TResult> continuationFunction)
779 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
782 public Task<TResult> ContinueWhenAll (Task[] tasks,
783 Func<Task[], TResult> continuationFunction,
784 TaskContinuationOptions continuationOptions)
786 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
789 public Task<TResult> ContinueWhenAll (Task[] tasks,
790 Func<Task[], TResult> continuationFunction,
791 CancellationToken cancellationToken)
793 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
796 public Task<TResult> ContinueWhenAll (Task[] tasks,
797 Func<Task[], TResult> continuationFunction,
798 CancellationToken cancellationToken,
799 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
801 return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
804 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
805 Func<Task<TAntecedentResult>[], TResult> continuationFunction)
807 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
810 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
811 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
812 TaskContinuationOptions continuationOptions)
814 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
817 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
818 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
819 CancellationToken cancellationToken)
821 return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
824 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
825 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
826 CancellationToken cancellationToken,
827 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
829 return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
835 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
837 [MonoLimitation(errorMsg)]
838 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
840 return FromAsync (asyncResult, endMethod, creationOptions);
843 [MonoLimitation(errorMsg)]
844 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
845 TaskCreationOptions creationOptions)
847 return FromAsync (asyncResult, endMethod, creationOptions);
850 [MonoLimitation(errorMsg)]
851 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
852 TaskCreationOptions creationOptions, TaskScheduler scheduler)
854 throw new NotSupportedException (errorMsg);
857 [MonoLimitation(errorMsg)]
858 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
859 Func<IAsyncResult, TResult> endMethod,
862 throw new NotSupportedException (errorMsg);
865 [MonoLimitation(errorMsg)]
866 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
867 Func<IAsyncResult, TResult> endMethod,
868 object state, TaskCreationOptions creationOptions)
870 throw new NotSupportedException (errorMsg);
873 [MonoLimitation(errorMsg)]
874 public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
875 Func<IAsyncResult, TResult> endMethod,
876 TArg1 arg1, object state)
878 throw new NotSupportedException (errorMsg);
881 [MonoLimitation(errorMsg)]
882 public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
883 Func<IAsyncResult, TResult> endMethod,
884 TArg1 arg1, object state, TaskCreationOptions creationOptions)
886 throw new NotSupportedException (errorMsg);
889 [MonoLimitation(errorMsg)]
890 public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
891 Func<IAsyncResult, TResult> endMethod,
892 TArg1 arg1, TArg2 arg2, object state)
894 throw new NotSupportedException (errorMsg);
897 [MonoLimitation(errorMsg)]
898 public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
899 Func<IAsyncResult, TResult> endMethod,
900 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
902 throw new NotSupportedException (errorMsg);
905 [MonoLimitation(errorMsg)]
906 public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
907 Func<IAsyncResult, TResult> endMethod,
908 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
910 throw new NotSupportedException (errorMsg);
913 [MonoLimitation(errorMsg)]
914 public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
915 Func<IAsyncResult, TResult> endMethod,
916 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
917 TaskCreationOptions creationOptions)
919 throw new NotSupportedException (errorMsg);
923 public TaskScheduler Scheduler {
929 public TaskContinuationOptions ContinuationOptions {
931 return continuationOptions;
935 public TaskCreationOptions CreationOptions {
937 return creationOptions;
941 public CancellationToken CancellationToken {
943 return cancellationToken;