2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Threading.Tasks / TaskFactory.cs
blob49fc5574d06d8bd05a31e33df84df93e48fb7aa6
1 #if NET_4_0
2 //
3 // TaskFactory.cs
4 //
5 // Author:
6 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 //
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
28 using System;
29 using System.Threading;
31 namespace System.Threading.Tasks
34 public class TaskFactory
36 TaskScheduler scheduler;
37 TaskCreationOptions options;
38 TaskContinuationOptions contOptions;
39 CancellationToken token;
41 #region ctors
42 public TaskFactory ()
43 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
47 public TaskFactory (CancellationToken token)
48 : this (token, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
52 public TaskFactory (TaskScheduler scheduler)
53 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
57 public TaskFactory (TaskCreationOptions options, TaskContinuationOptions contOptions)
58 : this (CancellationToken.None, options, contOptions, TaskScheduler.Current)
62 public TaskFactory (CancellationToken token, TaskCreationOptions options, TaskContinuationOptions contOptions,
63 TaskScheduler scheduler)
65 this.token = token;
66 this.scheduler = scheduler;
67 this.options = options;
68 this.contOptions = contOptions;
70 #endregion
72 #region StartNew for Task
73 public Task StartNew (Action action)
75 return StartNew (action, token, options, scheduler);
78 public Task StartNew (Action action, CancellationToken token)
80 return StartNew (action, token, options, scheduler);
83 public Task StartNew (Action action, TaskCreationOptions options)
85 return StartNew (action, token, options, scheduler);
88 public Task StartNew (Action<object> action, object state)
90 return StartNew (action, state, token, options, scheduler);
93 public Task StartNew (Action<object> action, object state, CancellationToken token)
95 return StartNew (action, state, token, options, scheduler);
98 public Task StartNew (Action<object> action, object state, TaskCreationOptions options)
100 return StartNew (action, state, token, options, scheduler);
103 public Task StartNew (Action action, CancellationToken token, TaskCreationOptions options, TaskScheduler scheduler)
105 return StartNew ((o) => action (), null, token, options, scheduler);
108 public Task StartNew (Action<object> action, object state, CancellationToken token, TaskCreationOptions options,
109 TaskScheduler scheduler)
111 Task t = new Task (action, state, options);
112 t.Start (scheduler);
114 return t;
116 #endregion
118 #region StartNew for Task<TResult>
119 public Task<TResult> StartNew<TResult> (Func<TResult> function)
121 return StartNew<TResult> (function, token, options, scheduler);
124 public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions options)
126 return StartNew<TResult> (function, token, options, scheduler);
130 public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken token)
132 return StartNew<TResult> (function, token, options, scheduler);
135 public Task<TResult> StartNew<TResult> (Func<TResult> function,
136 CancellationToken token,
137 TaskCreationOptions options,
138 TaskScheduler scheduler)
140 return StartNew<TResult> ((o) => function (), null, token, options, scheduler);
143 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
145 return StartNew<TResult> (function, state, token, options, scheduler);
148 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken token)
150 return StartNew<TResult> (function, state, token, options, scheduler);
153 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions options)
155 return StartNew<TResult> (function, state, token, options, scheduler);
158 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
159 CancellationToken token,
160 TaskCreationOptions options,
161 TaskScheduler scheduler)
163 Task<TResult> t = new Task<TResult> (function, state, token, options);
164 t.Start (scheduler);
166 return t;
168 #endregion
170 #region Continue
172 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
174 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
177 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken token)
179 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
182 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
184 return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
187 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken token,
188 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
190 AtomicBoolean trigger = new AtomicBoolean ();
191 Task commonContinuation = new Task (null);
193 foreach (Task t in tasks) {
194 Task cont = new Task ((o) => { continuationAction ((Task)o); }, t, token, options);
195 t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
196 cont.ContinueWithCore (commonContinuation, TaskContinuationOptions.None, scheduler);
199 return commonContinuation;
202 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction)
204 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
207 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
208 CancellationToken token)
210 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
213 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
214 TaskContinuationOptions continuationOptions)
216 return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
219 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>> continuationAction,
220 CancellationToken token, TaskContinuationOptions continuationOptions,
221 TaskScheduler scheduler)
223 return ContinueWhenAny ((Task[]) tasks, (o) => continuationAction ((Task<TAntecedentResult>)o),
224 token, continuationOptions, scheduler);
227 [MonoTODO]
228 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction)
230 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
233 [MonoTODO]
234 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
235 CancellationToken token)
237 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
240 [MonoTODO]
241 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
242 TaskContinuationOptions continuationOptions)
244 return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
247 [MonoTODO]
248 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
249 CancellationToken token,
250 TaskContinuationOptions continuationOptions,
251 TaskScheduler scheduler)
253 throw new NotImplementedException ();
256 [MonoTODO]
257 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
258 Func<Task<TAntecedentResult>, TResult> continuationAction)
260 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
263 [MonoTODO]
264 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
265 Func<Task<TAntecedentResult>, TResult> continuationAction,
266 CancellationToken token)
268 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
271 [MonoTODO]
272 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
273 Func<Task<TAntecedentResult>, TResult> continuationAction,
274 TaskContinuationOptions continuationOptions)
276 return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
279 [MonoTODO]
280 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
281 Func<Task<TAntecedentResult>, TResult> continuationAction,
282 CancellationToken token,
283 TaskContinuationOptions continuationOptions,
284 TaskScheduler scheduler)
286 return ContinueWhenAny<TResult> ((Task[])tasks, (t) => continuationAction((Task<TAntecedentResult>)t), token, continuationOptions, scheduler);
289 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction)
291 return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
294 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction, CancellationToken token)
296 return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
299 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction,
300 TaskContinuationOptions continuationOptions)
302 return ContinueWhenAll (tasks, continuationFunction, token, continuationOptions, scheduler);
305 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction, CancellationToken token,
306 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
308 CountdownEvent evt = new CountdownEvent (tasks.Length);
309 Task cont = new Task ((o) => continuationFunction ((Task[])o), tasks, token, options);
311 foreach (Task t in tasks)
312 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
314 return cont;
317 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
318 Action<Task<TAntecedentResult>[]> continuationFunction)
320 return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
323 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
324 Action<Task<TAntecedentResult>[]> continuationFunction, CancellationToken token)
326 return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
329 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationFunction,
330 TaskContinuationOptions continuationOptions)
332 return ContinueWhenAll (tasks, continuationFunction, token, continuationOptions, scheduler);
335 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
336 Action<Task<TAntecedentResult>[]> continuationFunction,
337 CancellationToken token, TaskContinuationOptions continuationOptions,
338 TaskScheduler scheduler)
340 return ContinueWhenAll ((Task[]) tasks, (o) => continuationFunction (tasks), token,
341 continuationOptions, scheduler);
344 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
346 return ContinueWhenAll<TResult> (tasks, continuationFunction, token, contOptions, scheduler);
349 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
350 TaskContinuationOptions continuationOptions)
352 return ContinueWhenAll<TResult> (tasks, continuationFunction, token, continuationOptions, scheduler);
355 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
356 CancellationToken token)
358 return ContinueWhenAll<TResult> (tasks, continuationFunction, token, contOptions, scheduler);
361 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
362 CancellationToken token,
363 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
365 CountdownEvent evt = new CountdownEvent (tasks.Length);
366 Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), tasks, token, options);
368 foreach (Task t in tasks)
369 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
371 return cont;
374 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
375 Func<Task<TAntecedentResult>[], TResult> continuationFunction)
377 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, token, contOptions, scheduler);
380 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
381 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
382 TaskContinuationOptions continuationOptions)
384 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, token, continuationOptions, scheduler);
387 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
388 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
389 CancellationToken token)
391 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, token, contOptions, scheduler);
394 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
395 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
396 CancellationToken token,
397 TaskContinuationOptions continuationOptions,
398 TaskScheduler scheduler)
400 return ContinueWhenAll<TResult> ((Task[]) tasks,
401 (o) => continuationFunction (tasks),
402 token,
403 continuationOptions, scheduler);
406 #endregion
408 #region FromAsync
409 // For these methods to work we first have to convert the ThreadPool to use Tasks as it
410 // is doing in 4.0, then all that is remaining is to identify the Task on which is
411 // run the async operation (probably with some additional state in a IAsyncResult subclass)
412 // and call its ContinueWith method accordingly
414 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
416 [MonoLimitation(errorMsg)]
417 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
419 return FromAsync (asyncResult, endMethod, options);
422 [MonoLimitation(errorMsg)]
423 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
424 TaskCreationOptions creationOptions)
426 return FromAsync (asyncResult, endMethod, creationOptions);
429 [MonoLimitation(errorMsg)]
430 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
431 TaskCreationOptions creationOptions, TaskScheduler scheduler)
433 throw new NotSupportedException (errorMsg);
436 [MonoLimitation(errorMsg)]
437 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
439 return FromAsync<TResult> (asyncResult, endMethod, options);
442 [MonoLimitation(errorMsg)]
443 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
444 TaskCreationOptions creationOptions)
446 return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
449 [MonoLimitation(errorMsg)]
450 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
451 TaskCreationOptions creationOptions, TaskScheduler scheduler)
453 throw new NotSupportedException (errorMsg);
457 [MonoLimitation(errorMsg)]
458 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
459 object state)
461 return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, options);
464 [MonoLimitation(errorMsg)]
465 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
466 object state, TaskCreationOptions creationOptions)
468 return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
471 [MonoLimitation(errorMsg)]
472 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
473 TArg1 arg1, object state)
475 throw new NotSupportedException (errorMsg);
478 [MonoLimitation(errorMsg)]
479 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
480 TArg1 arg1, object state, TaskCreationOptions creationOptions)
482 throw new NotSupportedException (errorMsg);
485 [MonoLimitation(errorMsg)]
486 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
487 TArg1 arg1, TArg2 arg2, object state)
489 throw new NotSupportedException (errorMsg);
492 [MonoLimitation(errorMsg)]
493 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
494 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
496 throw new NotSupportedException (errorMsg);
499 [MonoLimitation(errorMsg)]
500 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
501 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
503 throw new NotSupportedException (errorMsg);
506 [MonoLimitation(errorMsg)]
507 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
508 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
510 throw new NotSupportedException (errorMsg);
513 [MonoLimitation(errorMsg)]
514 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
515 Func<IAsyncResult, TResult> endMethod,
516 object state)
518 throw new NotSupportedException (errorMsg);
521 [MonoLimitation(errorMsg)]
522 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
523 Func<IAsyncResult, TResult> endMethod,
524 object state, TaskCreationOptions creationOptions)
526 throw new NotSupportedException (errorMsg);
529 [MonoLimitation(errorMsg)]
530 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
531 Func<IAsyncResult, TResult> endMethod,
532 TArg1 arg1, object state)
534 throw new NotSupportedException (errorMsg);
537 [MonoLimitation(errorMsg)]
538 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
539 Func<IAsyncResult, TResult> endMethod,
540 TArg1 arg1, object state, TaskCreationOptions creationOptions)
542 throw new NotSupportedException (errorMsg);
545 [MonoLimitation(errorMsg)]
546 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
547 Func<IAsyncResult, TResult> endMethod,
548 TArg1 arg1, TArg2 arg2, object state)
550 throw new NotSupportedException (errorMsg);
553 [MonoLimitation(errorMsg)]
554 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
555 Func<IAsyncResult, TResult> endMethod,
556 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
558 throw new NotSupportedException (errorMsg);
561 [MonoLimitation(errorMsg)]
562 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
563 Func<IAsyncResult, TResult> endMethod,
564 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
566 throw new NotSupportedException (errorMsg);
569 [MonoLimitation(errorMsg)]
570 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
571 Func<IAsyncResult, TResult> endMethod,
572 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
573 TaskCreationOptions creationOptions)
575 throw new NotSupportedException (errorMsg);
577 #endregion
579 public TaskScheduler Scheduler {
580 get {
581 return scheduler;
585 public TaskContinuationOptions ContinuationOptions {
586 get {
587 return contOptions;
591 public TaskCreationOptions CreationOptions {
592 get {
593 return options;
597 public CancellationToken CancellationToken {
598 get {
599 return token;
604 public class TaskFactory<TResult>
606 TaskScheduler scheduler;
607 TaskCreationOptions options;
608 TaskContinuationOptions contOptions;
609 CancellationToken token;
611 TaskFactory parent;
613 #region ctors
614 public TaskFactory ()
615 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
619 public TaskFactory (TaskScheduler scheduler)
620 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
624 public TaskFactory (CancellationToken token)
625 : this (token, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
629 public TaskFactory (TaskCreationOptions options, TaskContinuationOptions contOptions)
630 : this (CancellationToken.None, options, contOptions, TaskScheduler.Current)
634 public TaskFactory (CancellationToken token, TaskCreationOptions options, TaskContinuationOptions contOptions,
635 TaskScheduler scheduler)
637 this.token = token;
638 this.scheduler = scheduler;
639 this.options = options;
640 this.contOptions = contOptions;
642 this.parent = new TaskFactory (token, options, contOptions, scheduler);
645 #endregion
647 #region StartNew for Task<TResult>
648 public Task<TResult> StartNew (Func<TResult> function)
650 return StartNew (function, token, options, scheduler);
653 public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions options)
655 return StartNew (function, token, options, scheduler);
658 public Task<TResult> StartNew (Func<TResult> function, CancellationToken token)
660 return StartNew (function, token, options, scheduler);
663 public Task<TResult> StartNew (Func<TResult> function,
664 CancellationToken token,
665 TaskCreationOptions options,
666 TaskScheduler scheduler)
668 return StartNew ((o) => function (), null, token, options, scheduler);
671 public Task<TResult> StartNew (Func<object, TResult> function, object state)
673 return StartNew (function, state, token, options, scheduler);
676 public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions options)
678 return StartNew (function, state, token, options, scheduler);
681 public Task<TResult> StartNew (Func<object, TResult> function, object state, CancellationToken token)
683 return StartNew (function, state, token, options, scheduler);
686 public Task<TResult> StartNew (Func<object, TResult> function, object state,
687 CancellationToken token,
688 TaskCreationOptions options,
689 TaskScheduler scheduler)
691 return parent.StartNew<TResult> (function, state, token, options, scheduler);
693 #endregion
695 #region Continue
697 [MonoTODO]
698 public Task<TResult> ContinueWhenAny (Task[] tasks,
699 Func<Task, TResult> continuationAction)
701 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
704 [MonoTODO]
705 public Task<TResult> ContinueWhenAny (Task[] tasks,
706 Func<Task, TResult> continuationAction,
707 CancellationToken token)
709 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
712 [MonoTODO]
713 public Task<TResult> ContinueWhenAny (Task[] tasks,
714 Func<Task, TResult> continuationAction,
715 TaskContinuationOptions continuationOptions)
717 return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
720 [MonoTODO]
721 public Task<TResult> ContinueWhenAny (Task[] tasks,
722 Func<Task, TResult> continuationAction,
723 CancellationToken token,
724 TaskContinuationOptions continuationOptions,
725 TaskScheduler scheduler)
727 throw new NotImplementedException ();
730 [MonoTODO]
731 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
732 Func<Task<TAntecedentResult>, TResult> continuationAction)
734 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
737 [MonoTODO]
738 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
739 Func<Task<TAntecedentResult>, TResult> continuationAction,
740 CancellationToken token)
742 return ContinueWhenAny (tasks, continuationAction, token, contOptions, scheduler);
745 [MonoTODO]
746 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
747 Func<Task<TAntecedentResult>, TResult> continuationAction,
748 TaskContinuationOptions continuationOptions)
750 return ContinueWhenAny (tasks, continuationAction, token, continuationOptions, scheduler);
753 [MonoTODO]
754 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
755 Func<Task<TAntecedentResult>, TResult> continuationAction,
756 CancellationToken token,
757 TaskContinuationOptions continuationOptions,
758 TaskScheduler scheduler)
760 throw new NotImplementedException ();
763 public Task<TResult> ContinueWhenAll (Task[] tasks,
764 Func<Task[], TResult> continuationFunction)
766 return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
769 public Task<TResult> ContinueWhenAll (Task[] tasks,
770 Func<Task[], TResult> continuationFunction,
771 TaskContinuationOptions continuationOptions)
773 return ContinueWhenAll (tasks, continuationFunction, token, continuationOptions, scheduler);
776 public Task<TResult> ContinueWhenAll (Task[] tasks,
777 Func<Task[], TResult> continuationFunction,
778 CancellationToken token)
780 return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
783 public Task<TResult> ContinueWhenAll (Task[] tasks,
784 Func<Task[], TResult> continuationFunction,
785 CancellationToken token,
786 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
788 CountdownEvent evt = new CountdownEvent (tasks.Length);
789 Task<TResult> cont = new Task<TResult> ((o) => continuationFunction (tasks), tasks, token, options);
791 foreach (Task t in tasks)
792 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
794 return cont;
797 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
798 Func<Task<TAntecedentResult>[], TResult> continuationFunction)
800 return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
803 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
804 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
805 TaskContinuationOptions continuationOptions)
807 return ContinueWhenAll (tasks, continuationFunction, token, continuationOptions, scheduler);
810 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
811 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
812 CancellationToken token)
814 return ContinueWhenAll (tasks, continuationFunction, token, contOptions, scheduler);
817 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
818 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
819 CancellationToken token,
820 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
822 CountdownEvent evt = new CountdownEvent (tasks.Length);
823 Task<TResult> cont = new Task<TResult> ((o) => continuationFunction (tasks), tasks, token, options);
825 foreach (Task t in tasks)
826 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
828 return cont;
831 #endregion
833 #region FromAsync
834 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
836 [MonoLimitation(errorMsg)]
837 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
839 return FromAsync (asyncResult, endMethod, options);
842 [MonoLimitation(errorMsg)]
843 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
844 TaskCreationOptions creationOptions)
846 return FromAsync (asyncResult, endMethod, creationOptions);
849 [MonoLimitation(errorMsg)]
850 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
851 TaskCreationOptions creationOptions, TaskScheduler scheduler)
853 throw new NotSupportedException (errorMsg);
856 [MonoLimitation(errorMsg)]
857 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
858 Func<IAsyncResult, TResult> endMethod,
859 object state)
861 throw new NotSupportedException (errorMsg);
864 [MonoLimitation(errorMsg)]
865 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
866 Func<IAsyncResult, TResult> endMethod,
867 object state, TaskCreationOptions creationOptions)
869 throw new NotSupportedException (errorMsg);
872 [MonoLimitation(errorMsg)]
873 public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
874 Func<IAsyncResult, TResult> endMethod,
875 TArg1 arg1, object state)
877 throw new NotSupportedException (errorMsg);
880 [MonoLimitation(errorMsg)]
881 public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
882 Func<IAsyncResult, TResult> endMethod,
883 TArg1 arg1, object state, TaskCreationOptions creationOptions)
885 throw new NotSupportedException (errorMsg);
888 [MonoLimitation(errorMsg)]
889 public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
890 Func<IAsyncResult, TResult> endMethod,
891 TArg1 arg1, TArg2 arg2, object state)
893 throw new NotSupportedException (errorMsg);
896 [MonoLimitation(errorMsg)]
897 public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
898 Func<IAsyncResult, TResult> endMethod,
899 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
901 throw new NotSupportedException (errorMsg);
904 [MonoLimitation(errorMsg)]
905 public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
906 Func<IAsyncResult, TResult> endMethod,
907 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
909 throw new NotSupportedException (errorMsg);
912 [MonoLimitation(errorMsg)]
913 public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
914 Func<IAsyncResult, TResult> endMethod,
915 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
916 TaskCreationOptions creationOptions)
918 throw new NotSupportedException (errorMsg);
920 #endregion
922 public TaskScheduler Scheduler {
923 get {
924 return scheduler;
928 public TaskContinuationOptions ContinuationOptions {
929 get {
930 return contOptions;
934 public TaskCreationOptions CreationOptions {
935 get {
936 return options;
940 public CancellationToken CancellationToken {
941 get {
942 return token;
947 #endif