Sanitize parent-child relation in continuations
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskFactory.cs
1 // 
2 // TaskFactory.cs
3 //  
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
8 // 
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:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
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
25 // THE SOFTWARE.
26
27 #if NET_4_0
28
29 using System;
30 using System.Threading;
31
32 namespace System.Threading.Tasks
33 {
34         
35         public class TaskFactory
36         {
37                 TaskScheduler scheduler;
38                 TaskCreationOptions creationOptions;
39                 TaskContinuationOptions continuationOptions;
40                 CancellationToken cancellationToken;
41                 
42                 #region ctors
43                 public TaskFactory ()
44                         : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
45                 {
46                 }
47
48                 public TaskFactory (CancellationToken cancellationToken)
49                         : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
50                 {       
51                 }
52
53                 public TaskFactory (TaskScheduler scheduler)
54                         : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
55                 {       
56                 }
57                 
58                 public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
59                         : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
60                 {       
61                 }
62                 
63                 public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
64                                     TaskScheduler scheduler)
65                 {
66                         this.cancellationToken = cancellationToken;
67                         this.scheduler = scheduler;
68                         this.creationOptions = creationOptions;
69                         this.continuationOptions = continuationOptions;
70                 }
71                 #endregion
72                 
73                 #region StartNew for Task
74                 public Task StartNew (Action action)
75                 {
76                         return StartNew (action, cancellationToken, creationOptions, scheduler);
77                 }
78                 
79                 public Task StartNew (Action action, CancellationToken cancellationToken)
80                 {
81                         return StartNew (action, cancellationToken, creationOptions, scheduler);
82                 }
83                 
84                 public Task StartNew (Action action, TaskCreationOptions creationOptions)
85                 {
86                         return StartNew (action, cancellationToken, creationOptions, scheduler);
87                 }
88                 
89                 public Task StartNew (Action<object> action, object state)
90                 {
91                         return StartNew (action, state, cancellationToken, creationOptions, scheduler);
92                 }
93                 
94                 public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken)
95                 {
96                         return StartNew (action, state, cancellationToken, creationOptions, scheduler);
97                 }
98                 
99                 public Task StartNew (Action<object> action, object state, TaskCreationOptions creationOptions)
100                 {
101                         return StartNew (action, state, cancellationToken, creationOptions, scheduler);
102                 }
103                 
104                 public Task StartNew (Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
105                 {
106                         return StartNew ((o) => action (), null, cancellationToken, creationOptions, scheduler);
107                 }
108                 
109                 public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
110                                       TaskScheduler scheduler)
111                 {
112                         Task t = new Task (action, state, cancellationToken, creationOptions);
113                         t.Start (scheduler);
114                         
115                         return t;
116                 }
117                 #endregion
118                 
119                 #region StartNew for Task<TResult>      
120                 public Task<TResult> StartNew<TResult> (Func<TResult> function)
121                 {
122                         return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
123                 }
124                 
125                 public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
126                 {
127                         return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
128
129                 }
130                 
131                 public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
132                 {
133                         return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
134                 }
135                 
136                 public Task<TResult> StartNew<TResult> (Func<TResult> function,
137                                                         CancellationToken cancellationToken,
138                                                         TaskCreationOptions creationOptions,
139                                                         TaskScheduler scheduler)
140                 {
141                         return StartNew<TResult> ((o) => function (), null, cancellationToken, creationOptions, scheduler);
142                 }
143                 
144                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
145                 {
146                         return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
147                 }
148                 
149                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
150                 {
151                         return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
152                 }
153                 
154                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
155                 {
156                         return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
157                 }
158                 
159                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
160                                                         CancellationToken cancellationToken,
161                                                         TaskCreationOptions creationOptions,
162                                                         TaskScheduler scheduler)
163                 {
164                         Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
165                         t.Start (scheduler);
166                         
167                         return t;
168                 }
169                 #endregion
170                 
171                 #region Continue
172                 
173                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
174                 {
175                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
176                 }
177                 
178                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
179                 {
180                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
181                 }
182                 
183                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
184                 {
185                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
186                 }
187
188                 public Task ContinueWhenAny (Task[] tasks,
189                                              Action<Task> continuationAction,
190                                              CancellationToken cancellationToken,
191                                              TaskContinuationOptions continuationOptions,
192                                              TaskScheduler scheduler)
193                 {
194                         var ourTasks = (Task[])tasks.Clone ();
195                         AtomicBoolean trigger = new AtomicBoolean ();
196                         Task commonContinuation = new Task (null);
197                         
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);
202                         }
203                         
204                         return commonContinuation;
205                 }
206                 
207                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
208                                                                 Action<Task<TAntecedentResult>> continuationAction)
209                 {
210                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
211                 }
212                 
213                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
214                                                                 Action<Task<TAntecedentResult>> continuationAction,
215                                                                 CancellationToken cancellationToken)
216                 {
217                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
218                 }
219                 
220                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
221                                                                 Action<Task<TAntecedentResult>> continuationAction,
222                                                                 TaskContinuationOptions continuationOptions)
223                 {
224                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
225                 }
226
227                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
228                                                                 Action<Task<TAntecedentResult>> continuationAction,
229                                                                 CancellationToken cancellationToken,
230                                                                 TaskContinuationOptions continuationOptions,
231                                                                 TaskScheduler scheduler)
232                 {
233                         return ContinueWhenAny ((Task[]) tasks,
234                                                 (o) => continuationAction ((Task<TAntecedentResult>)o),
235                                                 cancellationToken, continuationOptions, scheduler);
236                 }
237
238                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
239                 {
240                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
241                 }
242
243                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
244                                                                Func<Task, TResult> continuationFunction,
245                                                                CancellationToken cancellationToken)
246                 {
247                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
248                 }
249
250                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
251                                                                Func<Task, TResult> continuationFunction,
252                                                                TaskContinuationOptions continuationOptions)
253                 {
254                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
255                 }
256
257                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
258                                                                Func<Task, TResult> continuationFunction,
259                                                                CancellationToken cancellationToken,
260                                                                TaskContinuationOptions continuationOptions,
261                                                                TaskScheduler scheduler)
262                 {
263                         var ourTasks = (Task[])tasks.Clone ();
264                         AtomicBoolean trigger = new AtomicBoolean ();
265                         TaskCompletionSource<TResult> source = new TaskCompletionSource<TResult> ();
266
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);
270                         }
271
272                         return source.Task;
273                 }
274
275                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
276                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction)
277                 {
278                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
279                 }
280
281                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
282                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction,
283                                                                                   CancellationToken cancellationToken)
284                 {
285                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
286                 }
287
288                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
289                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction,
290                                                                                   TaskContinuationOptions continuationOptions)
291                 {
292                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
293                 }
294
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)
300                 {
301                         return ContinueWhenAny<TResult> ((Task[])tasks,
302                                                          (t) => continuationFunction((Task<TAntecedentResult>)t),
303                                                          cancellationToken,
304                                                          continuationOptions,
305                                                          scheduler);
306                 }
307                 
308                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
309                 {
310                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
311                 }
312                 
313                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
314                 {
315                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
316                 }
317                 
318                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
319                                              TaskContinuationOptions continuationOptions)
320                 {
321                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
322                 }
323                 
324                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
325                                              TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
326                 {
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);
330                         
331                         foreach (Task t in ourTasks)
332                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
333                         
334                         return cont;
335                 }
336                 
337                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
338                                                                 Action<Task<TAntecedentResult>[]> continuationAction)
339                 {
340                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
341                 }
342                 
343                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
344                                                                 Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
345                 {
346                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
347                 }
348                 
349                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
350                                                                 TaskContinuationOptions continuationOptions)
351                 {
352                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
353                 }
354                 
355                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, 
356                                                                 Action<Task<TAntecedentResult>[]> continuationAction,
357                                                                 CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
358                                                                 TaskScheduler scheduler)
359                 {
360                         return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
361                                                 continuationOptions, scheduler);
362                 }
363                 
364                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
365                 {
366                         return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
367                 }
368                 
369                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
370                                                                TaskContinuationOptions continuationOptions)
371                 {
372                         return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
373                 }
374                 
375                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
376                                                                CancellationToken cancellationToken)
377                 {
378                         return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
379                 }
380                 
381                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
382                                                                CancellationToken cancellationToken,
383                                                                TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
384                 {
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);
388                         
389                         foreach (Task t in ourTasks)
390                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
391                         
392                         return cont;
393                 }
394                 
395                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
396                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction)
397                 {
398                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
399                 }
400                 
401                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
402                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
403                                                                                   TaskContinuationOptions continuationOptions)
404                 {
405                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
406                 }
407                 
408                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
409                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
410                                                                                   CancellationToken cancellationToken)
411                 {
412                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
413                 }
414                 
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)
420                 {
421                         return ContinueWhenAll<TResult> ((Task[]) tasks,
422                                                          (o) => continuationFunction (tasks),
423                                                          cancellationToken,
424                                                          continuationOptions, scheduler);
425                 }
426
427                 #endregion
428                 
429                 #region FromAsync
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
434                 
435                 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
436                 
437                 [MonoLimitation(errorMsg)]
438                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
439                 {
440                         return FromAsync (asyncResult, endMethod, creationOptions);
441                 }
442                 
443                 [MonoLimitation(errorMsg)]
444                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
445                                        TaskCreationOptions creationOptions)
446                 {
447                         return FromAsync (asyncResult, endMethod, creationOptions);
448                 }
449                 
450                 [MonoLimitation(errorMsg)]
451                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
452                                        TaskCreationOptions creationOptions, TaskScheduler scheduler)
453                 {
454                         throw new NotSupportedException (errorMsg);
455                 }
456                 
457                 [MonoLimitation(errorMsg)]
458                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
459                 {
460                         return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
461                 }
462                 
463                 [MonoLimitation(errorMsg)]
464                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
465                                                          TaskCreationOptions creationOptions)
466                 {
467                         return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
468                 }
469                 
470                 [MonoLimitation(errorMsg)]
471                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
472                                                          TaskCreationOptions creationOptions, TaskScheduler scheduler)
473                 {
474                         throw new NotSupportedException (errorMsg);
475                 }
476                 
477                 
478                 [MonoLimitation(errorMsg)]
479                 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
480                                        object state)
481                 {
482                         return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
483                 }
484                 
485                 [MonoLimitation(errorMsg)]
486                 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
487                                        object state, TaskCreationOptions creationOptions)
488                 {
489                         return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
490                 }
491                 
492                 [MonoLimitation(errorMsg)]
493                 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
494                                               TArg1 arg1, object state)
495                 {
496                         throw new NotSupportedException (errorMsg);
497                 }
498                 
499                 [MonoLimitation(errorMsg)]
500                 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
501                                               TArg1 arg1, object state, TaskCreationOptions creationOptions)
502                 {
503                         throw new NotSupportedException (errorMsg);
504                 }
505                 
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)
509                 {
510                         throw new NotSupportedException (errorMsg);
511                 }
512                 
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)
516                 {
517                         throw new NotSupportedException (errorMsg);
518                 }
519                 
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)
523                 {
524                         throw new NotSupportedException (errorMsg);
525                 }
526                 
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)
530                 {
531                         throw new NotSupportedException (errorMsg);
532                 }               
533                 
534                 [MonoLimitation(errorMsg)]
535                 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
536                                                          Func<IAsyncResult, TResult> endMethod,
537                                                          object state)
538                 {
539                         throw new NotSupportedException (errorMsg);
540                 }
541                 
542                 [MonoLimitation(errorMsg)]
543                 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
544                                                          Func<IAsyncResult, TResult> endMethod,
545                                        object state, TaskCreationOptions creationOptions)
546                 {
547                         throw new NotSupportedException (errorMsg);
548                 }
549                 
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)
554                 {
555                         throw new NotSupportedException (errorMsg);
556                 }
557                 
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)
562                 {
563                         throw new NotSupportedException (errorMsg);
564                 }
565                 
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)
570                 {
571                         throw new NotSupportedException (errorMsg);
572                 }
573                 
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)
578                 {
579                         throw new NotSupportedException (errorMsg);
580                 }
581                 
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)
586                 {
587                         throw new NotSupportedException (errorMsg);
588                 }
589                 
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)
595                 {
596                         throw new NotSupportedException (errorMsg);
597                 }
598                 #endregion
599                 
600                 public TaskScheduler Scheduler {
601                         get {
602                                 return scheduler;
603                         }
604                 }
605                 
606                 public TaskContinuationOptions ContinuationOptions {
607                         get {
608                                 return continuationOptions;
609                         }
610                 }
611                 
612                 public TaskCreationOptions CreationOptions {
613                         get {
614                                 return creationOptions;
615                         }
616                 }
617                 
618                 public CancellationToken CancellationToken {
619                         get {
620                                 return cancellationToken;
621                         }
622                 }
623         }
624         
625         public class TaskFactory<TResult>
626         {
627                 TaskScheduler scheduler;
628                 TaskCreationOptions creationOptions;
629                 TaskContinuationOptions continuationOptions;
630                 CancellationToken cancellationToken;
631                 
632                 TaskFactory parent;
633                 
634                 #region ctors
635                 public TaskFactory ()
636                         : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
637                 {       
638                 }
639                 
640                 public TaskFactory (TaskScheduler scheduler)
641                         : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
642                 {       
643                 }
644                 
645                 public TaskFactory (CancellationToken cancellationToken)
646                         : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
647                 {       
648                 }
649                 
650                 public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
651                         : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
652                 {       
653                 }
654                 
655                 public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
656                                     TaskScheduler scheduler)
657                 {
658                         this.cancellationToken = cancellationToken;
659                         this.scheduler = scheduler;
660                         this.creationOptions = creationOptions;
661                         this.continuationOptions = continuationOptions;
662                         
663                         this.parent = new TaskFactory (cancellationToken, creationOptions, continuationOptions, scheduler);
664                 }
665                 
666                 #endregion
667                 
668                 #region StartNew for Task<TResult>      
669                 public Task<TResult> StartNew (Func<TResult> function)
670                 {
671                         return StartNew (function, cancellationToken, creationOptions, scheduler);
672                 }
673                 
674                 public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions creationOptions)
675                 {
676                         return StartNew (function, cancellationToken, creationOptions, scheduler);
677                 }
678                 
679                 public Task<TResult> StartNew (Func<TResult> function, CancellationToken cancellationToken)
680                 {
681                         return StartNew (function, cancellationToken, creationOptions, scheduler);
682                 }
683                 
684                 public Task<TResult> StartNew (Func<TResult> function, 
685                                                CancellationToken cancellationToken,
686                                                TaskCreationOptions creationOptions,
687                                                TaskScheduler scheduler)
688                 {
689                         return StartNew ((o) => function (), null, cancellationToken, creationOptions, scheduler);
690                 }
691                 
692                 public Task<TResult> StartNew (Func<object, TResult> function, object state)
693                 {
694                         return StartNew (function, state, cancellationToken, creationOptions, scheduler);
695                 }
696                 
697                 public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
698                 {
699                         return StartNew (function, state, cancellationToken, creationOptions, scheduler);
700                 }
701                 
702                 public Task<TResult> StartNew (Func<object, TResult> function, object state, CancellationToken cancellationToken)
703                 {
704                         return StartNew (function, state, cancellationToken, creationOptions, scheduler);
705                 }
706                 
707                 public Task<TResult> StartNew (Func<object, TResult> function, object state, 
708                                                CancellationToken cancellationToken,
709                                                TaskCreationOptions creationOptions,
710                                                TaskScheduler scheduler)
711                 {
712                         return parent.StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
713                 }
714                 #endregion
715                 
716                 #region Continue
717
718                 public Task<TResult> ContinueWhenAny (Task[] tasks,
719                                                       Func<Task, TResult> continuationFunction)
720                 {
721                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
722                 }
723
724                 public Task<TResult> ContinueWhenAny (Task[] tasks,
725                                                       Func<Task, TResult> continuationFunction,
726                                                       CancellationToken cancellationToken)
727                 {
728                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
729                 }
730
731                 public Task<TResult> ContinueWhenAny (Task[] tasks,
732                                                       Func<Task, TResult> continuationFunction,
733                                                       TaskContinuationOptions continuationOptions)
734                 {
735                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
736                 }
737
738                 public Task<TResult> ContinueWhenAny (Task[] tasks,
739                                                       Func<Task, TResult> continuationFunction,
740                                                       CancellationToken cancellationToken,
741                                                       TaskContinuationOptions continuationOptions,
742                                                       TaskScheduler scheduler)
743                 {
744                         return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
745                 }
746
747                 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
748                                                                          Func<Task<TAntecedentResult>, TResult> continuationFunction)
749                 {
750                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
751                 }
752
753                 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
754                                                                          Func<Task<TAntecedentResult>, TResult> continuationFunction,
755                                                                          CancellationToken cancellationToken)
756                 {
757                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
758                 }
759
760                 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
761                                                                          Func<Task<TAntecedentResult>, TResult> continuationFunction,
762                                                                          TaskContinuationOptions continuationOptions)
763                 {
764                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
765                 }
766
767                 public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
768                                                                          Func<Task<TAntecedentResult>, TResult> continuationFunction,
769                                                                          CancellationToken cancellationToken,
770                                                                          TaskContinuationOptions continuationOptions,
771                                                                          TaskScheduler scheduler)
772                 {
773                         return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
774                 }
775                 
776                 public Task<TResult> ContinueWhenAll (Task[] tasks,
777                                                       Func<Task[], TResult> continuationFunction)
778                 {
779                         return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
780                 }
781                 
782                 public Task<TResult> ContinueWhenAll (Task[] tasks,
783                                                       Func<Task[], TResult> continuationFunction,
784                                                       TaskContinuationOptions continuationOptions)
785                 {
786                         return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
787                 }
788                 
789                 public Task<TResult> ContinueWhenAll (Task[] tasks,
790                                                       Func<Task[], TResult> continuationFunction,
791                                                       CancellationToken cancellationToken)
792                 {
793                         return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
794                 }
795                 
796                 public Task<TResult> ContinueWhenAll (Task[] tasks,
797                                                       Func<Task[], TResult> continuationFunction,
798                                                       CancellationToken cancellationToken,
799                                                       TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
800                 {
801                         return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
802                 }
803                 
804                 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
805                                                                          Func<Task<TAntecedentResult>[], TResult> continuationFunction)
806                 {
807                         return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
808                 }
809                 
810                 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
811                                                                          Func<Task<TAntecedentResult>[], TResult> continuationFunction,
812                                                                          TaskContinuationOptions continuationOptions)
813                 {
814                         return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
815                 }
816                 
817                 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
818                                                                          Func<Task<TAntecedentResult>[], TResult> continuationFunction,
819                                                                          CancellationToken cancellationToken)
820                 {
821                         return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
822                 }
823                 
824                 public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
825                                                                          Func<Task<TAntecedentResult>[], TResult> continuationFunction,
826                                                                          CancellationToken cancellationToken,
827                                                                          TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
828                 {
829                         return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
830                 }
831
832                 #endregion
833                 
834                 #region FromAsync
835                 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
836                 
837                 [MonoLimitation(errorMsg)]
838                 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
839                 {
840                         return FromAsync (asyncResult, endMethod, creationOptions);
841                 }
842                 
843                 [MonoLimitation(errorMsg)]
844                 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
845                                                 TaskCreationOptions creationOptions)
846                 {
847                         return FromAsync (asyncResult, endMethod, creationOptions);
848                 }
849                 
850                 [MonoLimitation(errorMsg)]
851                 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
852                                                 TaskCreationOptions creationOptions, TaskScheduler scheduler)
853                 {
854                         throw new NotSupportedException (errorMsg);
855                 }
856                 
857                 [MonoLimitation(errorMsg)]
858                 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
859                                                 Func<IAsyncResult, TResult> endMethod,
860                                                 object state)
861                 {
862                         throw new NotSupportedException (errorMsg);
863                 }
864                 
865                 [MonoLimitation(errorMsg)]
866                 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
867                                                 Func<IAsyncResult, TResult> endMethod,
868                                                 object state, TaskCreationOptions creationOptions)
869                 {
870                         throw new NotSupportedException (errorMsg);
871                 }
872                 
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)
877                 {
878                         throw new NotSupportedException (errorMsg);
879                 }
880                 
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)
885                 {
886                         throw new NotSupportedException (errorMsg);
887                 }
888                 
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)
893                 {
894                         throw new NotSupportedException (errorMsg);
895                 }
896                 
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)
901                 {
902                         throw new NotSupportedException (errorMsg);
903                 }
904                 
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)
909                 {
910                         throw new NotSupportedException (errorMsg);
911                 }
912                 
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)
918                 {
919                         throw new NotSupportedException (errorMsg);
920                 }
921                 #endregion
922                 
923                 public TaskScheduler Scheduler {
924                         get {
925                                 return scheduler;
926                         }
927                 }
928                 
929                 public TaskContinuationOptions ContinuationOptions {
930                         get {
931                                 return continuationOptions;
932                         }
933                 }
934                 
935                 public TaskCreationOptions CreationOptions {
936                         get {
937                                 return creationOptions;
938                         }
939                 }
940                 
941                 public CancellationToken CancellationToken {
942                         get {
943                                 return cancellationToken;
944                         }
945                 }
946         }
947 }
948 #endif