2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskFactory.cs
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.
27
28 using System;
29 using System.Threading;
30
31 namespace System.Threading.Tasks
32 {
33         
34         public class TaskFactory
35         {
36                 TaskScheduler scheduler;
37                 TaskCreationOptions options;
38                 TaskContinuationOptions contOptions;            
39                 
40                 #region ctors
41                 public TaskFactory () : this (TaskScheduler.Current, TaskCreationOptions.None, TaskContinuationOptions.None)
42                 {       
43                 }
44                 
45                 public TaskFactory (TaskScheduler scheduler) : this (scheduler, TaskCreationOptions.None, TaskContinuationOptions.None)
46                 {       
47                 }
48                 
49                 public TaskFactory (TaskCreationOptions options, TaskContinuationOptions contOptions)
50                         : this (TaskScheduler.Current, options, contOptions)
51                 {       
52                 }
53                 
54                 public TaskFactory (TaskScheduler scheduler, TaskCreationOptions options, TaskContinuationOptions contOptions)
55                 {
56                         this.scheduler = scheduler;
57                         this.options = options;
58                         this.contOptions = contOptions;
59                 }
60                 #endregion
61                 
62                 #region StartNew for Task
63                 public Task StartNew (Action action)
64                 {
65                         return StartNew (action, options, scheduler);
66                 }
67                 
68                 public Task StartNew (Action action, TaskCreationOptions options)
69                 {
70                         return StartNew (action, options, scheduler);
71                 }
72                 
73                 public Task StartNew (Action action, TaskCreationOptions options, TaskScheduler scheduler)
74                 {
75                         return StartNew ((o) => action (), null, options, scheduler);
76                 }
77                 
78                 public Task StartNew (Action<object> action, object state)
79                 {
80                         return StartNew (action, state, options, scheduler);
81                 }
82                 
83                 public Task StartNew (Action<object> action, object state, TaskCreationOptions options)
84                 {
85                         return StartNew (action, state, options, scheduler);
86                 }
87                 
88                 public Task StartNew (Action<object> action, object state, TaskCreationOptions options, TaskScheduler scheduler)
89                 {
90                         Task t = new Task (action, state, options);
91                         t.Start (scheduler);
92                         
93                         return t;
94                 }
95                 #endregion
96                 
97                 #region StartNew for Task<TResult>      
98                 public Task<TResult> StartNew<TResult> (Func<TResult> function)
99                 {
100                         return StartNew<TResult> (function, options, scheduler);
101                 }
102                 
103                 public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions options)
104                 {
105                         return StartNew<TResult> (function, options, scheduler);
106                 }
107                 
108                 public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions options, TaskScheduler scheduler)
109                 {
110                         return StartNew<TResult> ((o) => function (), null, options, scheduler);
111                 }
112                 
113                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
114                 {
115                         return StartNew<TResult> (function, state, options, scheduler);
116                 }
117                 
118                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions options)
119                 {
120                         return StartNew<TResult> (function, state, options, scheduler);
121                 }
122                 
123                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions options,
124                                                         TaskScheduler scheduler)
125                 {
126                         Task<TResult> t = new Task<TResult> (function, state, options);
127                         t.Start (scheduler);
128                         
129                         return t;
130                 }
131                 #endregion
132                 
133                 #region Continue
134                 
135                 [MonoTODO]
136                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
137                 {
138                         return ContinueWhenAny (tasks, continuationAction, contOptions, scheduler);
139                 }
140                 
141                 [MonoTODO]
142                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
143                 {
144                         return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
145                 }
146
147                 [MonoTODO]
148                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions,
149                                              TaskScheduler scheduler)
150                 {
151                         throw new NotImplementedException ();
152                 }
153                 
154                 [MonoTODO]
155                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction)
156                 {
157                         return ContinueWhenAny (tasks, continuationAction, contOptions);
158                 }
159                 
160                 [MonoTODO]
161                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
162                                                                TaskContinuationOptions continuationOptions)
163                 {
164                         return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
165                 }
166
167                 [MonoTODO]
168                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
169                                                                TaskContinuationOptions continuationOptions,
170                                                                TaskScheduler scheduler)
171                 {
172                         throw new NotImplementedException ();
173                 }
174                 
175                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction)
176                 {
177                         return ContinueWhenAll (tasks, continuationFunction, contOptions);
178                 }
179                 
180                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction,
181                                              TaskContinuationOptions continuationOptions)
182                 {
183                         return ContinueWhenAll (tasks, continuationFunction, continuationOptions, scheduler);
184                 }
185                 
186                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction,
187                                              TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
188                 {
189                         CountdownEvent evt = new CountdownEvent (tasks.Length);
190                         Task cont = new Task ((o) => continuationFunction ((Task[])o), tasks, options);
191                         
192                         foreach (Task t in tasks)
193                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
194                         
195                         return cont;
196                 }
197
198                 
199                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
200                 {
201                         return ContinueWhenAll<TResult> (tasks, continuationFunction, contOptions);
202                 }
203                 
204                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
205                                                                TaskContinuationOptions continuationOptions)
206                 {
207                         return ContinueWhenAll<TResult> (tasks, continuationFunction, continuationOptions, scheduler);
208                 }
209                 
210                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
211                                                                TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
212                 {
213                         CountdownEvent evt = new CountdownEvent (tasks.Length);
214                         Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), tasks, options);
215                         
216                         foreach (Task t in tasks)
217                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
218                         
219                         return cont;
220                 }
221
222                 #endregion
223                 
224                 #region FromAsync
225                 // For these methods to work we first have to convert the ThreadPool to use Tasks as it
226                 // is doing in 4.0, then all that is remaining is to identify the Task on which is 
227                 // run the async operation (probably with some additional state in a IAsyncResult subclass)
228                 // and call its ContinueWith method accordingly
229                 
230                 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
231                 
232                 [MonoLimitation(errorMsg)]
233                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
234                 {
235                         return FromAsync (asyncResult, endMethod, options);
236                 }
237                 
238                 [MonoLimitation(errorMsg)]
239                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
240                                        TaskCreationOptions creationOptions)
241                 {
242                         return FromAsync (asyncResult, endMethod, creationOptions);
243                 }
244                 
245                 [MonoLimitation(errorMsg)]
246                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
247                                        TaskCreationOptions creationOptions, TaskScheduler scheduler)
248                 {
249                         throw new NotSupportedException (errorMsg);
250                 }
251                 
252                 [MonoLimitation(errorMsg)]
253                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
254                 {
255                         return FromAsync<TResult> (asyncResult, endMethod, options);
256                 }
257                 
258                 [MonoLimitation(errorMsg)]
259                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
260                                                          TaskCreationOptions creationOptions)
261                 {
262                         return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
263                 }
264                 
265                 [MonoLimitation(errorMsg)]
266                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
267                                                          TaskCreationOptions creationOptions, TaskScheduler scheduler)
268                 {
269                         throw new NotSupportedException (errorMsg);
270                 }
271                 
272                 
273                 [MonoLimitation(errorMsg)]
274                 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
275                                        object state)
276                 {
277                         return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, options);
278                 }
279                 
280                 [MonoLimitation(errorMsg)]
281                 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
282                                        object state, TaskCreationOptions creationOptions)
283                 {
284                         return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
285                 }
286                 
287                 [MonoLimitation(errorMsg)]
288                 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
289                                               TArg1 arg1, object state)
290                 {
291                         throw new NotSupportedException (errorMsg);
292                 }
293                 
294                 [MonoLimitation(errorMsg)]
295                 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
296                                               TArg1 arg1, object state, TaskCreationOptions creationOptions)
297                 {
298                         throw new NotSupportedException (errorMsg);
299                 }
300                 
301                 [MonoLimitation(errorMsg)]
302                 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
303                                                      TArg1 arg1, TArg2 arg2, object state)
304                 {
305                         throw new NotSupportedException (errorMsg);
306                 }
307                 
308                 [MonoLimitation(errorMsg)]
309                 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
310                                                      TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
311                 {
312                         throw new NotSupportedException (errorMsg);
313                 }
314                 
315                 [MonoLimitation(errorMsg)]
316                 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
317                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
318                 {
319                         throw new NotSupportedException (errorMsg);
320                 }
321                 
322                 [MonoLimitation(errorMsg)]
323                 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
324                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
325                 {
326                         throw new NotSupportedException (errorMsg);
327                 }               
328                 
329                 [MonoLimitation(errorMsg)]
330                 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
331                                                          Func<IAsyncResult, TResult> endMethod,
332                                                          object state)
333                 {
334                         throw new NotSupportedException (errorMsg);
335                 }
336                 
337                 [MonoLimitation(errorMsg)]
338                 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
339                                                          Func<IAsyncResult, TResult> endMethod,
340                                        object state, TaskCreationOptions creationOptions)
341                 {
342                         throw new NotSupportedException (errorMsg);
343                 }
344                 
345                 [MonoLimitation(errorMsg)]
346                 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
347                                                                 Func<IAsyncResult, TResult> endMethod,
348                                                                 TArg1 arg1, object state)
349                 {
350                         throw new NotSupportedException (errorMsg);
351                 }
352                 
353                 [MonoLimitation(errorMsg)]
354                 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
355                                                              Func<IAsyncResult, TResult> endMethod,
356                                                              TArg1 arg1, object state, TaskCreationOptions creationOptions)
357                 {
358                         throw new NotSupportedException (errorMsg);
359                 }
360                 
361                 [MonoLimitation(errorMsg)]
362                 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
363                                                                        Func<IAsyncResult, TResult> endMethod,
364                                                                        TArg1 arg1, TArg2 arg2, object state)
365                 {
366                         throw new NotSupportedException (errorMsg);
367                 }
368                 
369                 [MonoLimitation(errorMsg)]
370                 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
371                                                                        Func<IAsyncResult, TResult> endMethod,
372                                                                        TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
373                 {
374                         throw new NotSupportedException (errorMsg);
375                 }
376                 
377                 [MonoLimitation(errorMsg)]
378                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
379                                                                               Func<IAsyncResult, TResult> endMethod,
380                                                                               TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
381                 {
382                         throw new NotSupportedException (errorMsg);
383                 }
384                 
385                 [MonoLimitation(errorMsg)]
386                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
387                                                                               Func<IAsyncResult, TResult> endMethod,
388                                                                               TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
389                                                                               TaskCreationOptions creationOptions)
390                 {
391                         throw new NotSupportedException (errorMsg);
392                 }
393                 #endregion
394                 
395                 public TaskScheduler Scheduler {
396                         get {
397                                 return scheduler;
398                         }
399                 }
400                 
401                 public TaskContinuationOptions ContinuationOptions {
402                         get {
403                                 return contOptions;
404                         }
405                 }
406                 
407                 public TaskCreationOptions CreationOptions {
408                         get {
409                                 return options;
410                         }
411                 }
412         }
413         
414         public class TaskFactory<TResult>
415         {
416                 TaskScheduler scheduler;
417                 TaskCreationOptions options;
418                 TaskContinuationOptions contOptions;
419                 
420                 TaskFactory parent;
421                 
422                 #region ctors
423                 public TaskFactory () : this (TaskScheduler.Current, TaskCreationOptions.None, TaskContinuationOptions.None)
424                 {       
425                 }
426                 
427                 public TaskFactory (TaskScheduler scheduler) : this (scheduler, TaskCreationOptions.None, TaskContinuationOptions.None)
428                 {       
429                 }
430                 
431                 public TaskFactory (TaskCreationOptions options, TaskContinuationOptions contOptions)
432                         : this (TaskScheduler.Current, options, contOptions)
433                 {       
434                 }
435                 
436                 public TaskFactory (TaskScheduler scheduler, TaskCreationOptions options, TaskContinuationOptions contOptions)
437                 {
438                         this.scheduler = scheduler;
439                         this.options = options;
440                         this.contOptions = contOptions;
441                         this.parent = new TaskFactory (scheduler, options, contOptions);
442                 }
443                 #endregion
444                 
445                 #region StartNew for Task<TResult>      
446                 public Task<TResult> StartNew (Func<TResult> function)
447                 {
448                         return StartNew (function, options, scheduler);
449                 }
450                 
451                 public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions options)
452                 {
453                         return StartNew (function, options, scheduler);
454                 }
455                 
456                 public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions options, TaskScheduler scheduler)
457                 {
458                         return StartNew ((o) => function (), null, options, scheduler);
459                 }
460                 
461                 public Task<TResult> StartNew (Func<object, TResult> function, object state)
462                 {
463                         return StartNew (function, state, options, scheduler);
464                 }
465                 
466                 public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions options)
467                 {
468                         return StartNew (function, state, options, scheduler);
469                 }
470                 
471                 public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions options,
472                                                TaskScheduler scheduler)
473                 {
474                         return parent.StartNew<TResult> (function, state, options, scheduler);
475                 }
476                 #endregion
477                 
478                 #region Continue
479                 [MonoTODO]
480                 public Task ContinueWhenAny (Task<TResult>[] tasks, Action<Task<TResult>> continuationAction)
481                 {
482                         return ContinueWhenAny (tasks, continuationAction, contOptions, scheduler);
483                 }
484                 
485                 [MonoTODO]
486                 public Task ContinueWhenAny (Task<TResult>[] tasks, Action<Task<TResult>> continuationAction,
487                                              TaskContinuationOptions continuationOptions)
488                 {
489                         return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
490                 }
491
492                 [MonoTODO]
493                 public Task ContinueWhenAny (Task<TResult>[] tasks, Action<Task<TResult>> continuationAction,
494                                              TaskContinuationOptions continuationOptions,
495                                              TaskScheduler scheduler)
496                 {
497                         throw new NotImplementedException ();
498                 }
499                 
500                 [MonoTODO]
501                 public Task<TNewResult> ContinueWhenAny<TNewResult> (Task<TResult>[] tasks, Func<Task<TResult>, TNewResult> continuationAction)
502                 {
503                         return ContinueWhenAny (tasks, continuationAction, contOptions);
504                 }
505                 
506                 [MonoTODO]
507                 public Task<TNewResult> ContinueWhenAny<TNewResult> (Task<TResult>[] tasks, Func<Task<TResult>, TNewResult> continuationAction,
508                                                                      TaskContinuationOptions continuationOptions)
509                 {
510                         return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
511                 }
512
513                 [MonoTODO]
514                 public Task<TNewResult> ContinueWhenAny<TNewResult> (Task<TResult>[] tasks, Func<Task<TResult>, TNewResult> continuationAction,
515                                                                      TaskContinuationOptions continuationOptions,
516                                                                      TaskScheduler scheduler)
517                 {
518                         throw new NotImplementedException ();
519                 }
520                 
521                 public Task ContinueWhenAll (Task<TResult>[] tasks, Action<Task<TResult>[]> continuationFunction)
522                 {
523                         return ContinueWhenAll (tasks, continuationFunction, contOptions);
524                 }
525                 
526                 public Task ContinueWhenAll (Task<TResult>[] tasks, Action<Task<TResult>[]> continuationFunction,
527                                              TaskContinuationOptions continuationOptions)
528                 {
529                         return ContinueWhenAll (tasks, continuationFunction, continuationOptions, scheduler);
530                 }
531                 
532                 public Task ContinueWhenAll (Task<TResult>[] tasks, Action<Task<TResult>[]> continuationFunction,
533                                              TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
534                 {
535                         CountdownEvent evt = new CountdownEvent (tasks.Length);
536                         Task cont = new Task ((o) => continuationFunction ((Task<TResult>[])o), tasks, options);
537                         
538                         foreach (Task t in tasks)
539                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
540                         
541                         return cont;
542                 }
543                 
544                 public Task<TNewResult> ContinueWhenAll<TNewResult> (Task<TResult>[] tasks,
545                                                                      Func<Task<TResult>[], TNewResult> continuationFunction)
546                 {
547                         return ContinueWhenAll (tasks, continuationFunction, contOptions);
548                 }
549                 
550                 public Task<TNewResult> ContinueWhenAll<TNewResult> (Task<TResult>[] tasks,
551                                                                      Func<Task<TResult>[], TNewResult> continuationFunction,
552                                                                      TaskContinuationOptions continuationOptions)
553                 {
554                         return ContinueWhenAll (tasks, continuationFunction, continuationOptions, scheduler);
555                 }
556                 
557                 public Task<TNewResult> ContinueWhenAll<TNewResult> (Task<TResult>[] tasks,
558                                                                      Func<Task<TResult>[], TNewResult> continuationFunction,
559                                                                      TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
560                 {
561                         CountdownEvent evt = new CountdownEvent (tasks.Length);
562                         Task<TNewResult> cont = new Task<TNewResult> ((o) => continuationFunction ((Task<TResult>[])o), tasks, options);
563                         
564                         foreach (Task t in tasks)
565                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
566                         
567                         return cont;
568                 }
569
570                 #endregion
571                 
572                 #region FromAsync
573                 const string errorMsg = "Mono's thread pool doesn't support this operation yet";
574                 
575                 [MonoLimitation(errorMsg)]
576                 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
577                 {
578                         return FromAsync (asyncResult, endMethod, options);
579                 }
580                 
581                 [MonoLimitation(errorMsg)]
582                 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
583                                                 TaskCreationOptions creationOptions)
584                 {
585                         return FromAsync (asyncResult, endMethod, creationOptions);
586                 }
587                 
588                 [MonoLimitation(errorMsg)]
589                 public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
590                                                 TaskCreationOptions creationOptions, TaskScheduler scheduler)
591                 {
592                         throw new NotSupportedException (errorMsg);
593                 }
594                 
595                 [MonoLimitation(errorMsg)]
596                 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
597                                                 Func<IAsyncResult, TResult> endMethod,
598                                                 object state)
599                 {
600                         throw new NotSupportedException (errorMsg);
601                 }
602                 
603                 [MonoLimitation(errorMsg)]
604                 public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
605                                                 Func<IAsyncResult, TResult> endMethod,
606                                                 object state, TaskCreationOptions creationOptions)
607                 {
608                         throw new NotSupportedException (errorMsg);
609                 }
610                 
611                 [MonoLimitation(errorMsg)]
612                 public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
613                                                        Func<IAsyncResult, TResult> endMethod,
614                                                        TArg1 arg1, object state)
615                 {
616                         throw new NotSupportedException (errorMsg);
617                 }
618                 
619                 [MonoLimitation(errorMsg)]
620                 public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
621                                                        Func<IAsyncResult, TResult> endMethod,
622                                                        TArg1 arg1, object state, TaskCreationOptions creationOptions)
623                 {
624                         throw new NotSupportedException (errorMsg);
625                 }
626                 
627                 [MonoLimitation(errorMsg)]
628                 public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
629                                                               Func<IAsyncResult, TResult> endMethod,
630                                                               TArg1 arg1, TArg2 arg2, object state)
631                 {
632                         throw new NotSupportedException (errorMsg);
633                 }
634                 
635                 [MonoLimitation(errorMsg)]
636                 public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
637                                                               Func<IAsyncResult, TResult> endMethod,
638                                                               TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
639                 {
640                         throw new NotSupportedException (errorMsg);
641                 }
642                 
643                 [MonoLimitation(errorMsg)]
644                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
645                                                                      Func<IAsyncResult, TResult> endMethod,
646                                                                      TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
647                 {
648                         throw new NotSupportedException (errorMsg);
649                 }
650                 
651                 [MonoLimitation(errorMsg)]
652                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
653                                                                      Func<IAsyncResult, TResult> endMethod,
654                                                                      TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
655                                                                      TaskCreationOptions creationOptions)
656                 {
657                         throw new NotSupportedException (errorMsg);
658                 }
659                 #endregion
660                 
661                 public TaskScheduler Scheduler {
662                         get {
663                                 return scheduler;
664                         }
665                 }
666                 
667                 public TaskContinuationOptions ContinuationOptions {
668                         get {
669                                 return contOptions;
670                         }
671                 }
672                 
673                 public TaskCreationOptions CreationOptions {
674                         get {
675                                 return options;
676                         }
677                 }
678         }
679 }
680 #endif