Merge pull request #231 from linquize/a853199c497bb0977970974303fac7e42080809d
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskFactory.cs
1 // 
2 // TaskFactory.cs
3 //  
4 // Authors:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 //       Marek Safar <marek.safar@gmail.com>
7 // 
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10 // 
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28
29 #if NET_4_0 || MOBILE
30
31 namespace System.Threading.Tasks
32 {
33         public class TaskFactory
34         {
35                 readonly TaskScheduler scheduler;
36                 TaskCreationOptions creationOptions;
37                 TaskContinuationOptions continuationOptions;
38                 CancellationToken cancellationToken;
39                 
40                 public TaskFactory ()
41                         : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, null)
42                 {
43                 }
44
45                 public TaskFactory (CancellationToken cancellationToken)
46                         : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
47                 {       
48                 }
49
50                 public TaskFactory (TaskScheduler scheduler)
51                         : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
52                 {       
53                 }
54                 
55                 public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
56                         : this (CancellationToken.None, creationOptions, continuationOptions, null)
57                 {       
58                 }
59                 
60                 public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
61                                     TaskScheduler scheduler)
62                 {
63                         this.cancellationToken = cancellationToken;
64                         this.scheduler = scheduler;
65                         this.creationOptions = creationOptions;
66                         this.continuationOptions = continuationOptions;
67
68                         CheckContinuationOptions (continuationOptions);
69                 }
70                 
71                 public TaskScheduler Scheduler {
72                         get {
73                                 return scheduler;
74                         }
75                 }
76                 
77                 public TaskContinuationOptions ContinuationOptions {
78                         get {
79                                 return continuationOptions;
80                         }
81                 }
82                 
83                 public TaskCreationOptions CreationOptions {
84                         get {
85                                 return creationOptions;
86                         }
87                 }
88                 
89                 public CancellationToken CancellationToken {
90                         get {
91                                 return cancellationToken;
92                         }
93                 }
94
95                 internal static void CheckContinuationOptions (TaskContinuationOptions continuationOptions)
96                 {
97                         if ((continuationOptions & (TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.NotOnRanToCompletion)) != 0)
98                                 throw new ArgumentOutOfRangeException ("continuationOptions");
99
100                         const TaskContinuationOptions long_running = TaskContinuationOptions.LongRunning | TaskContinuationOptions.ExecuteSynchronously;
101                         if ((continuationOptions & long_running) == long_running)
102                                 throw new ArgumentOutOfRangeException ("continuationOptions", "Synchronous continuations cannot be long running");
103                 }
104                 
105                 #region StartNew for Task
106                 public Task StartNew (Action action)
107                 {
108                         return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
109                 }
110                 
111                 public Task StartNew (Action action, CancellationToken cancellationToken)
112                 {
113                         return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
114                 }
115                 
116                 public Task StartNew (Action action, TaskCreationOptions creationOptions)
117                 {
118                         return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
119                 }
120
121                 public Task StartNew (Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
122                 {
123                         Task t = new Task (action, cancellationToken, creationOptions);
124
125                         //
126                         // Don't start cancelled task it would throw an exception
127                         //
128                         if (!t.IsCompleted)
129                                 t.Start (scheduler);
130
131                         return t;
132                 }
133                 
134                 public Task StartNew (Action<object> action, object state)
135                 {
136                         return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
137                 }
138                 
139                 public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken)
140                 {
141                         return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
142                 }
143                 
144                 public Task StartNew (Action<object> action, object state, TaskCreationOptions creationOptions)
145                 {
146                         return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
147                 }
148                 
149                 public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
150                                       TaskScheduler scheduler)
151                 {
152                         Task t = new Task (action, state, cancellationToken, creationOptions);
153
154                         //
155                         // Don't start cancelled task it would throw an exception
156                         //
157                         if (!t.IsCompleted)
158                                 t.Start (scheduler);
159                         
160                         return t;
161                 }
162                 #endregion
163                 
164                 #region StartNew for Task<TResult>      
165                 public Task<TResult> StartNew<TResult> (Func<TResult> function)
166                 {
167                         return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
168                 }
169                 
170                 public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
171                 {
172                         return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
173
174                 }
175                 
176                 public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
177                 {
178                         return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
179                 }
180                 
181                 public Task<TResult> StartNew<TResult> (Func<TResult> function,
182                                                         CancellationToken cancellationToken,
183                                                         TaskCreationOptions creationOptions,
184                                                         TaskScheduler scheduler)
185                 {
186                         var t = new Task<TResult> (function, cancellationToken, creationOptions);
187
188                         //
189                         // Don't start cancelled task it would throw an exception
190                         //
191                         if (!t.IsCompleted)
192                                 t.Start (scheduler);
193
194                         return t;
195                 }
196                 
197                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
198                 {
199                         return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
200                 }
201                 
202                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
203                 {
204                         return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
205                 }
206                 
207                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
208                 {
209                         return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
210                 }
211                 
212                 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
213                                                         CancellationToken cancellationToken,
214                                                         TaskCreationOptions creationOptions,
215                                                         TaskScheduler scheduler)
216                 {
217                         Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
218                         t.Start (scheduler);
219                         
220                         return t;
221                 }
222                 #endregion
223                 
224                 #region Continue
225                 
226                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
227                 {
228                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
229                 }
230                 
231                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
232                 {
233                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
234                 }
235                 
236                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
237                 {
238                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
239                 }
240
241                 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
242                 {
243                         if (tasks == null)
244                                 throw new ArgumentNullException ("tasks");
245
246                         if (tasks.Length == 0)
247                                 throw new ArgumentException ("The tasks argument contains no tasks", "tasks");
248
249                         foreach (var ta in tasks) {
250                                 if (ta == null)
251                                         throw new ArgumentException ("The tasks argument constains a null value", "tasks");
252                         }
253
254                         if (continuationAction == null)
255                                 throw new ArgumentNullException ("continuationAction");
256
257                         var t = new Task<int> (l => {
258                                 var data = (Tuple<Task[], CancellationToken>) l;
259                                 return Task.WaitAny (data.Item1, data.Item2);
260                         }, Tuple.Create (tasks, cancellationToken));
261
262                         var cont = t.ContinueWith (TaskActionInvoker.Create (continuationAction, tasks), cancellationToken, continuationOptions, scheduler);
263
264                         t.Start (scheduler);
265
266                         return cont;
267                 }
268                 
269                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
270                                                                 Action<Task<TAntecedentResult>> continuationAction)
271                 {
272                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
273                 }
274                 
275                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
276                                                                 Action<Task<TAntecedentResult>> continuationAction,
277                                                                 CancellationToken cancellationToken)
278                 {
279                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
280                 }
281                 
282                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
283                                                                 Action<Task<TAntecedentResult>> continuationAction,
284                                                                 TaskContinuationOptions continuationOptions)
285                 {
286                         return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
287                 }
288
289                 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
290                                                                 Action<Task<TAntecedentResult>> continuationAction,
291                                                                 CancellationToken cancellationToken,
292                                                                 TaskContinuationOptions continuationOptions,
293                                                                 TaskScheduler scheduler)
294                 {
295                         return ContinueWhenAny ((Task[]) tasks,
296                                                 (o) => continuationAction ((Task<TAntecedentResult>)o),
297                                                 cancellationToken, continuationOptions, scheduler);
298                 }
299
300                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
301                 {
302                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
303                 }
304
305                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
306                                                                Func<Task, TResult> continuationFunction,
307                                                                CancellationToken cancellationToken)
308                 {
309                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
310                 }
311
312                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
313                                                                Func<Task, TResult> continuationFunction,
314                                                                TaskContinuationOptions continuationOptions)
315                 {
316                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
317                 }
318
319                 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
320                                                                Func<Task, TResult> continuationFunction,
321                                                                CancellationToken cancellationToken,
322                                                                TaskContinuationOptions continuationOptions,
323                                                                TaskScheduler scheduler)
324                 {
325                         var t = new Task<int> (l => {
326                                 var data = (Tuple<Task[], CancellationToken>) l;
327                                 return Task.WaitAny (data.Item1, data.Item2);
328                         }, Tuple.Create (tasks, cancellationToken));
329
330                         var cont = t.ContinueWith<TResult> (TaskActionInvoker.Create (continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);
331
332                         t.Start (scheduler);
333
334                         return cont;
335                 }
336
337                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
338                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction)
339                 {
340                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
341                 }
342
343                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
344                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction,
345                                                                                   CancellationToken cancellationToken)
346                 {
347                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
348                 }
349
350                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
351                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction,
352                                                                                   TaskContinuationOptions continuationOptions)
353                 {
354                         return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
355                 }
356
357                 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
358                                                                                   Func<Task<TAntecedentResult>, TResult> continuationFunction,
359                                                                                   CancellationToken cancellationToken,
360                                                                                   TaskContinuationOptions continuationOptions,
361                                                                                   TaskScheduler scheduler)
362                 {
363                         return ContinueWhenAny<TResult> ((Task[])tasks,
364                                                          (t) => continuationFunction((Task<TAntecedentResult>)t),
365                                                          cancellationToken,
366                                                          continuationOptions,
367                                                          scheduler);
368                 }
369                 
370                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
371                 {
372                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
373                 }
374                 
375                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
376                 {
377                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
378                 }
379                 
380                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
381                                              TaskContinuationOptions continuationOptions)
382                 {
383                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
384                 }
385                 
386                 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
387                                              TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
388                 {
389                         var t = new Task (l => {
390                                 var data = (Tuple<Task[], CancellationToken>) l;
391                                 Task.WaitAll (data.Item1, data.Item2);
392                         }, Tuple.Create (tasks, cancellationToken));
393
394                         var cont = t.ContinueWith (TaskActionInvoker.Create (continuationAction, tasks), cancellationToken, continuationOptions, scheduler);
395
396                         t.Start (scheduler);
397
398                         return cont;
399                 }
400                 
401                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
402                                                                 Action<Task<TAntecedentResult>[]> continuationAction)
403                 {
404                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
405                 }
406                 
407                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
408                                                                 Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
409                 {
410                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
411                 }
412                 
413                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
414                                                                 TaskContinuationOptions continuationOptions)
415                 {
416                         return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
417                 }
418                 
419                 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, 
420                                                                 Action<Task<TAntecedentResult>[]> continuationAction,
421                                                                 CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
422                                                                 TaskScheduler scheduler)
423                 {
424                         return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
425                                                 continuationOptions, scheduler);
426                 }
427                 
428                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
429                 {
430                         return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
431                 }
432                 
433                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
434                                                                TaskContinuationOptions continuationOptions)
435                 {
436                         return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
437                 }
438                 
439                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
440                                                                CancellationToken cancellationToken)
441                 {
442                         return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
443                 }
444                 
445                 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
446                                                                CancellationToken cancellationToken,
447                                                                TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
448                 {
449                         var t = new Task (l => {
450                                 var data = (Tuple<Task[], CancellationToken>) l;
451                                 Task.WaitAll (data.Item1, data.Item2);
452                         }, Tuple.Create (tasks, cancellationToken));
453
454                         var cont = t.ContinueWith<TResult> (TaskActionInvoker.Create (continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);
455
456                         t.Start (scheduler);
457
458                         return cont;
459                 }
460                 
461                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
462                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction)
463                 {
464                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
465                 }
466                 
467                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
468                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
469                                                                                   TaskContinuationOptions continuationOptions)
470                 {
471                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
472                 }
473                 
474                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
475                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
476                                                                                   CancellationToken cancellationToken)
477                 {
478                         return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
479                 }
480                 
481                 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
482                                                                                   Func<Task<TAntecedentResult>[], TResult> continuationFunction,
483                                                                                   CancellationToken cancellationToken,
484                                                                                   TaskContinuationOptions continuationOptions,
485                                                                                   TaskScheduler scheduler)
486                 {
487                         return ContinueWhenAll<TResult> ((Task[]) tasks,
488                                                          (o) => continuationFunction (tasks),
489                                                          cancellationToken,
490                                                          continuationOptions, scheduler);
491                 }
492
493                 #endregion
494
495                 #region FromAsync IAsyncResult
496                 
497                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
498                 {
499                         return FromAsync (asyncResult, endMethod, creationOptions);
500                 }
501                 
502                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions)
503                 {
504                         return FromAsync (asyncResult, endMethod, creationOptions, GetScheduler ());
505                 }
506
507                 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
508                 {
509                         if (endMethod == null)
510                                 throw new ArgumentNullException ("endMethod");
511
512                         return TaskFactory<object>.FromIAsyncResult (asyncResult,
513                                 l => {
514                                         endMethod (asyncResult);
515                                         return null;
516                                 }, creationOptions, scheduler);
517                 }
518                 
519                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
520                 {
521                         return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
522                 }
523                 
524                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
525                 {
526                         return FromAsync<TResult> (asyncResult, endMethod, creationOptions, GetScheduler ());
527                 }
528                 
529                 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
530                 {
531                         return TaskFactory<TResult>.FromIAsyncResult (asyncResult, endMethod, creationOptions, scheduler);
532                 }
533
534                 #endregion
535
536                 #region FromAsync Begin/End Method
537
538                 public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod, object state)
539                 {
540                         return FromAsync (beginMethod, endMethod, state, creationOptions);
541                 }
542
543                 public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
544                                                            object state, TaskCreationOptions creationOptions)
545                 {
546                         return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
547                                 l => { endMethod (l); return null; },
548                                 state, creationOptions);
549                 }
550
551                 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
552                                                   TArg1 arg1, object state)
553                 {
554                         return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
555                 }
556
557                 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
558                                               TArg1 arg1, object state, TaskCreationOptions creationOptions)
559                 {
560                         if (endMethod == null)
561                                 throw new ArgumentNullException ("endMethod");
562
563                         return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
564                                 l => { endMethod (l); return null; },
565                                 arg1, state, creationOptions);
566                 }
567
568                 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
569                                                      Action<IAsyncResult> endMethod,
570                                                      TArg1 arg1, TArg2 arg2, object state)
571                 {
572                         return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
573                 }
574
575                 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
576                                                      Action<IAsyncResult> endMethod,
577                                                      TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
578                 {
579                         if (endMethod == null)
580                                 throw new ArgumentNullException ("endMethod");
581
582                         return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
583                                 l => { endMethod (l); return null; },
584                                 arg1, arg2, state, creationOptions);
585                 }
586
587                 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
588                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
589                 {
590                         return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
591                 }
592
593                 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
594                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
595                 {
596                         if (endMethod == null)
597                                 throw new ArgumentNullException ("endMethod");
598
599                         return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
600                                 l => { endMethod (l); return null; },
601                                 arg1, arg2, arg3, state, creationOptions);
602                 }
603
604                 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
605                                                          object state)
606                 {
607                         return FromAsync (beginMethod, endMethod, state, creationOptions);
608                 }
609
610                 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
611                                                          object state, TaskCreationOptions creationOptions)
612                 {
613                         return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, state, creationOptions);
614                 }
615
616                 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
617                                                                 TArg1 arg1, object state)
618                 {
619                         return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
620                 }
621
622                 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
623                                                                 TArg1 arg1, object state, TaskCreationOptions creationOptions)
624                 {
625                         return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, state, creationOptions);
626                 }
627
628                 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
629                                                                        Func<IAsyncResult, TResult> endMethod,
630                                                                        TArg1 arg1, TArg2 arg2, object state)
631                 {
632                         return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
633                 }
634
635                 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
636                                                                        TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
637                 {
638                         return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, state, creationOptions);
639                 }
640
641                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
642                                                                               TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
643                 {
644                         return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
645                 }
646
647                 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
648                                                                               TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
649                 {
650                         return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
651                 }
652
653                 #endregion
654
655                 TaskScheduler GetScheduler ()
656                 {
657                         return scheduler ?? TaskScheduler.Current;
658                 }
659         }
660 }
661 #endif