Split internal types in their own files
[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 || MOBILE
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                         Task t = new Task (action, cancellationToken, creationOptions);
107                         t.Start (scheduler);
108
109                         return t;
110                 }
111                 
112                 public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
113                                       TaskScheduler scheduler)
114                 {
115                         Task t = new Task (action, state, cancellationToken, creationOptions);
116                         t.Start (scheduler);
117                         
118                         return t;
119                 }
120                 #endregion
121                 
122                 #region StartNew for Task<TResult>      
123                 public Task<TResult> StartNew<TResult> (Func<TResult> function)
124                 {
125                         return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
126                 }
127                 
128                 public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
129                 {
130                         return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
131
132                 }
133                 
134                 public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
135                 {
136                         return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
137                 }
138                 
139                 public Task<TResult> StartNew<TResult> (Func<TResult> function,
140                                                         CancellationToken cancellationToken,
141                                                         TaskCreationOptions creationOptions,
142                                                         TaskScheduler scheduler)
143                 {
144                         return StartNew<TResult> ((o) => function (), null, cancellationToken, creationOptions, scheduler);
145                 }
146                 
147                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
148                 {
149                         return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
150                 }
151                 
152                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
153                 {
154                         return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
155                 }
156                 
157                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
158                 {
159                         return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
160                 }
161                 
162                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
163                                                         CancellationToken cancellationToken,
164                                                         TaskCreationOptions creationOptions,
165                                                         TaskScheduler scheduler)
166                 {
167                         Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
168                         t.Start (scheduler);
169                         
170                         return t;
171                 }
172                 #endregion
173                 
174                 #region Continue
175                 
176                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
177                 {
178                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
179                 }
180                 
181                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
182                 {
183                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
184                 }
185                 
186                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
187                 {
188                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
189                 }
190
191                 public Task ContinueWhenAny (Task[] tasks,
192                                              Action<Task> continuationAction,
193                                              CancellationToken cancellationToken,
194                                              TaskContinuationOptions continuationOptions,
195                                              TaskScheduler scheduler)
196                 {
197                         var ourTasks = (Task[])tasks.Clone ();
198                         AtomicBoolean trigger = new AtomicBoolean ();
199                         Task commonContinuation = new Task (null);
200                         
201                         foreach (Task t in ourTasks) {
202                                 Task cont = new Task ((o) => continuationAction ((Task)o), t, cancellationToken, creationOptions, t);
203                                 t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
204                                 cont.ContinueWithCore (commonContinuation, TaskContinuationOptions.None, scheduler);
205                         }
206                         
207                         return commonContinuation;
208                 }
209                 
210                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
211                                                                 Action<Task<TAntecedentResult>> continuationAction)
212                 {
213                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
214                 }
215                 
216                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
217                                                                 Action<Task<TAntecedentResult>> continuationAction,
218                                                                 CancellationToken cancellationToken)
219                 {
220                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
221                 }
222                 
223                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
224                                                                 Action<Task<TAntecedentResult>> continuationAction,
225                                                                 TaskContinuationOptions continuationOptions)
226                 {
227                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
228                 }
229
230                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
231                                                                 Action<Task<TAntecedentResult>> continuationAction,
232                                                                 CancellationToken cancellationToken,
233                                                                 TaskContinuationOptions continuationOptions,
234                                                                 TaskScheduler scheduler)
235                 {
236                         return ContinueWhenAny ((Task[]) tasks,
237                                                 (o) => continuationAction ((Task<TAntecedentResult>)o),
238                                                 cancellationToken, continuationOptions, scheduler);
239                 }
240
241                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
242                 {
243                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
244                 }
245
246                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
247                                                                Func<Task, TResult> continuationFunction,
248                                                                CancellationToken cancellationToken)
249                 {
250                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
251                 }
252
253                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
254                                                                Func<Task, TResult> continuationFunction,
255                                                                TaskContinuationOptions continuationOptions)
256                 {
257                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
258                 }
259
260                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
261                                                                Func<Task, TResult> continuationFunction,
262                                                                CancellationToken cancellationToken,
263                                                                TaskContinuationOptions continuationOptions,
264                                                                TaskScheduler scheduler)
265                 {
266                         var ourTasks = (Task[])tasks.Clone ();
267                         AtomicBoolean trigger = new AtomicBoolean ();
268                         TaskCompletionSource<TResult> source = new TaskCompletionSource<TResult> ();
269
270                         foreach (Task t in ourTasks) {
271                                 Task cont = new Task ((o) => source.SetResult (continuationFunction ((Task)o)), t, cancellationToken, creationOptions, t);
272                                 t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
273                         }
274
275                         return source.Task;
276                 }
277
278                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
279                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction)
280                 {
281                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
282                 }
283
284                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
285                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction,
286                                                                                   CancellationToken cancellationToken)
287                 {
288                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
289                 }
290
291                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
292                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction,
293                                                                                   TaskContinuationOptions continuationOptions)
294                 {
295                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
296                 }
297
298                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
299                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction,
300                                                                                   CancellationToken cancellationToken,
301                                                                                   TaskContinuationOptions continuationOptions,
302                                                                                   TaskScheduler scheduler)
303                 {
304                         return ContinueWhenAny<TResult> ((Task[])tasks,
305                                                          (t) => continuationFunction((Task<TAntecedentResult>)t),
306                                                          cancellationToken,
307                                                          continuationOptions,
308                                                          scheduler);
309                 }
310                 
311                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
312                 {
313                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
314                 }
315                 
316                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
317                 {
318                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
319                 }
320                 
321                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
322                                              TaskContinuationOptions continuationOptions)
323                 {
324                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
325                 }
326                 
327                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
328                                              TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
329                 {
330                         var ourTasks = (Task[])tasks.Clone ();
331                         CountdownEvent evt = new CountdownEvent (ourTasks.Length);
332                         Task cont = new Task ((o) => continuationAction ((Task[])o), ourTasks, cancellationToken, creationOptions);
333                         
334                         foreach (Task t in ourTasks)
335                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
336                         
337                         return cont;
338                 }
339                 
340                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
341                                                                 Action<Task<TAntecedentResult>[]> continuationAction)
342                 {
343                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
344                 }
345                 
346                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
347                                                                 Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
348                 {
349                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
350                 }
351                 
352                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
353                                                                 TaskContinuationOptions continuationOptions)
354                 {
355                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
356                 }
357                 
358                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, 
359                                                                 Action<Task<TAntecedentResult>[]> continuationAction,
360                                                                 CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
361                                                                 TaskScheduler scheduler)
362                 {
363                         return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
364                                                 continuationOptions, scheduler);
365                 }
366                 
367                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
368                 {
369                         return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
370                 }
371                 
372                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
373                                                                TaskContinuationOptions continuationOptions)
374                 {
375                         return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
376                 }
377                 
378                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
379                                                                CancellationToken cancellationToken)
380                 {
381                         return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
382                 }
383                 
384                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
385                                                                CancellationToken cancellationToken,
386                                                                TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
387                 {
388                         var ourTasks = (Task[])tasks.Clone ();
389                         CountdownEvent evt = new CountdownEvent (ourTasks.Length);
390                         Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), ourTasks, cancellationToken, creationOptions);
391                         
392                         foreach (Task t in ourTasks)
393                                 t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
394                         
395                         return cont;
396                 }
397                 
398                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
399                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction)
400                 {
401                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
402                 }
403                 
404                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
405                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
406                                                                                   TaskContinuationOptions continuationOptions)
407                 {
408                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
409                 }
410                 
411                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
412                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
413                                                                                   CancellationToken cancellationToken)
414                 {
415                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
416                 }
417                 
418                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
419                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
420                                                                                   CancellationToken cancellationToken,
421                                                                                   TaskContinuationOptions continuationOptions,
422                                                                                   TaskScheduler scheduler)
423                 {
424                         return ContinueWhenAll<TResult> ((Task[]) tasks,
425                                                          (o) => continuationFunction (tasks),
426                                                          cancellationToken,
427                                                          continuationOptions, scheduler);
428                 }
429
430                 #endregion
431                 
432                 #region FromAsync
433                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
434                 {
435                         return FromAsync (asyncResult, endMethod, creationOptions, scheduler);
436                 }
437                 
438                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
439                                        TaskCreationOptions creationOptions)
440                 {
441                         return FromAsync (asyncResult, endMethod, creationOptions, scheduler);
442                 }
443                 
444                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
445                                        TaskCreationOptions creationOptions, TaskScheduler scheduler)
446                 {
447                         return FromAsync<object> (asyncResult, (ar) => { endMethod (ar); return null; }, creationOptions, scheduler);
448                 }
449                 
450                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
451                 {
452                         return FromAsync<TResult> (asyncResult, endMethod, creationOptions, scheduler);
453                 }
454                 
455                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
456                                                          TaskCreationOptions creationOptions)
457                 {
458                         return FromAsync<TResult> (asyncResult, endMethod, creationOptions, scheduler);
459                 }
460                 
461                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
462                                                          TaskCreationOptions creationOptions, TaskScheduler scheduler)
463                 {
464                         var completionSource = new TaskCompletionSource<TResult> ();
465
466                         ThreadPool.RegisterWaitForSingleObject (asyncResult.AsyncWaitHandle,
467                                                                 (o, b) => {
468                                                                         try {
469                                                                                 completionSource.SetResult (endMethod (asyncResult));
470                                                                         } catch (Exception e) {
471                                                                                 completionSource.SetException (e);
472                                                                         }
473                                                                 },
474                                                                 null,
475                                                                 -1,
476                                                                 true);
477
478                         return completionSource.Task;
479                 }
480                 
481                 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
482                                        Action<IAsyncResult> endMethod,
483                                        object state)
484                 {
485                         return FromAsync (beginMethod, endMethod, state, creationOptions);
486                 }
487                 
488                 public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
489                                        Action<IAsyncResult> endMethod,
490                                        object state, TaskCreationOptions creationOptions)
491                 {
492                         return FromAsync (beginMethod, (ar) => { endMethod (ar); return (object)null; }, state, creationOptions);
493                 }
494                 
495                 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
496                                               TArg1 arg1, object state)
497                 {
498                         return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
499                 }
500                 
501                 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
502                                               TArg1 arg1, object state, TaskCreationOptions creationOptions)
503                 {
504                         return FromAsync (beginMethod, (ar) => { endMethod (ar); return (object)null; }, arg1, state, creationOptions);
505                 }
506                 
507                 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
508                                                      Action<IAsyncResult> endMethod,
509                                                      TArg1 arg1, TArg2 arg2, object state)
510                 {
511                         return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
512                 }
513                 
514                 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
515                                                      Action<IAsyncResult> endMethod,
516                                                      TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
517                 {
518                         return FromAsync (beginMethod, (ar) => { endMethod (ar); return (object)null; }, arg1, arg2, state, creationOptions);
519                 }
520                 
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                         return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
525                 }
526                 
527                 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
528                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
529                 {
530                         return FromAsync (beginMethod, (ar) => { endMethod (ar); return (object)null; }, arg1, arg2, arg3, state, creationOptions);
531                 }               
532                 
533                 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
534                                                          Func<IAsyncResult, TResult> endMethod,
535                                                          object state)
536                 {
537                         return FromAsync (beginMethod, endMethod, state, creationOptions);
538                 }
539                 
540                 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
541                                                          Func<IAsyncResult, TResult> endMethod,
542                                                          object state, TaskCreationOptions creationOptions)
543                 {
544                         var completionSource = new TaskCompletionSource<TResult> (creationOptions);
545                         beginMethod ((ar) => {
546                                 try {
547                                         completionSource.SetResult (endMethod (ar));
548                                 } catch (Exception e) {
549                                         completionSource.SetException (e);
550                                 }
551                         }, state);
552
553                         return completionSource.Task;
554                 }
555                 
556                 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
557                                                                 Func<IAsyncResult, TResult> endMethod,
558                                                                 TArg1 arg1, object state)
559                 {
560                         return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
561                 }
562                 
563                 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
564                                                                 Func<IAsyncResult, TResult> endMethod,
565                                                                 TArg1 arg1, object state, TaskCreationOptions creationOptions)
566                 {
567                         var completionSource = new TaskCompletionSource<TResult> (creationOptions);
568                         beginMethod (arg1, (ar) => {
569                                 try {
570                                         completionSource.SetResult (endMethod (ar));
571                                 } catch (Exception e) {
572                                         completionSource.SetException (e);
573                                 }
574                         }, state);
575
576                         return completionSource.Task;
577                 }
578
579                 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
580                                                                        Func<IAsyncResult, TResult> endMethod,
581                                                                        TArg1 arg1, TArg2 arg2, object state)
582                 {
583                         return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
584                 }
585                 
586                 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
587                                                                        Func<IAsyncResult, TResult> endMethod,
588                                                                        TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
589                 {
590                         var completionSource = new TaskCompletionSource<TResult> (creationOptions);
591                         beginMethod (arg1, arg2, (ar) => {
592                                 try {
593                                         completionSource.SetResult (endMethod (ar));
594                                 } catch (Exception e) {
595                                         completionSource.SetException (e);
596                                 }
597                         }, state);
598
599                         return completionSource.Task;
600                 }
601                 
602                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
603                                                                               Func<IAsyncResult, TResult> endMethod,
604                                                                               TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
605                 {
606                         return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
607                 }
608                 
609                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
610                                                                               Func<IAsyncResult, TResult> endMethod,
611                                                                               TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
612                                                                               TaskCreationOptions creationOptions)
613                 {
614                         var completionSource = new TaskCompletionSource<TResult> (creationOptions);
615                         beginMethod (arg1, arg2, arg3, (ar) => {
616                                 try {
617                                         completionSource.SetResult (endMethod (ar));
618                                 } catch (Exception e) {
619                                         completionSource.SetException (e);
620                                 }
621                         }, state);
622
623                         return completionSource.Task;
624                 }
625                 #endregion
626                 
627                 public TaskScheduler Scheduler {
628                         get {
629                                 return scheduler;
630                         }
631                 }
632                 
633                 public TaskContinuationOptions ContinuationOptions {
634                         get {
635                                 return continuationOptions;
636                         }
637                 }
638                 
639                 public TaskCreationOptions CreationOptions {
640                         get {
641                                 return creationOptions;
642                         }
643                 }
644                 
645                 public CancellationToken CancellationToken {
646                         get {
647                                 return cancellationToken;
648                         }
649                 }
650         }
651 }
652 #endif