5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // Marek Safar <marek.safar@gmail.com>
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
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:
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
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
31 namespace System.Threading.Tasks
33 public class TaskFactory
35 readonly TaskScheduler scheduler;
36 TaskCreationOptions creationOptions;
37 TaskContinuationOptions continuationOptions;
38 CancellationToken cancellationToken;
41 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, null)
45 public TaskFactory (CancellationToken cancellationToken)
46 : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
50 public TaskFactory (TaskScheduler scheduler)
51 : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
55 public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
56 : this (CancellationToken.None, creationOptions, continuationOptions, null)
60 public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
61 TaskScheduler scheduler)
63 this.cancellationToken = cancellationToken;
64 this.scheduler = scheduler;
65 this.creationOptions = creationOptions;
66 this.continuationOptions = continuationOptions;
68 CheckContinuationOptions (continuationOptions);
71 public TaskScheduler Scheduler {
77 public TaskContinuationOptions ContinuationOptions {
79 return continuationOptions;
83 public TaskCreationOptions CreationOptions {
85 return creationOptions;
89 public CancellationToken CancellationToken {
91 return cancellationToken;
95 internal static void CheckContinuationOptions (TaskContinuationOptions continuationOptions)
97 if ((continuationOptions & (TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.NotOnRanToCompletion)) != 0)
98 throw new ArgumentOutOfRangeException ("continuationOptions");
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");
105 #region StartNew for Task
106 public Task StartNew (Action action)
108 return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
111 public Task StartNew (Action action, CancellationToken cancellationToken)
113 return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
116 public Task StartNew (Action action, TaskCreationOptions creationOptions)
118 return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
121 public Task StartNew (Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
123 Task t = new Task (action, cancellationToken, creationOptions);
126 // Don't start cancelled task it would throw an exception
134 public Task StartNew (Action<object> action, object state)
136 return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
139 public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken)
141 return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
144 public Task StartNew (Action<object> action, object state, TaskCreationOptions creationOptions)
146 return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
149 public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
150 TaskScheduler scheduler)
152 Task t = new Task (action, state, cancellationToken, creationOptions);
155 // Don't start cancelled task it would throw an exception
164 #region StartNew for Task<TResult>
165 public Task<TResult> StartNew<TResult> (Func<TResult> function)
167 return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
170 public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
172 return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
176 public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
178 return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
181 public Task<TResult> StartNew<TResult> (Func<TResult> function,
182 CancellationToken cancellationToken,
183 TaskCreationOptions creationOptions,
184 TaskScheduler scheduler)
186 var t = new Task<TResult> (function, cancellationToken, creationOptions);
189 // Don't start cancelled task it would throw an exception
197 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
199 return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
202 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
204 return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
207 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
209 return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
212 public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
213 CancellationToken cancellationToken,
214 TaskCreationOptions creationOptions,
215 TaskScheduler scheduler)
217 var t = new Task<TResult> (function, state, cancellationToken, creationOptions);
220 // Don't start cancelled task it would throw an exception
231 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
233 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
236 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
238 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
241 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
243 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
246 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
248 CheckContinueArguments (tasks, continuationAction, continuationOptions, scheduler);
250 var cont = Task.WhenAnyCore (tasks).ContinueWith (TaskActionInvoker.CreateSelected (continuationAction), cancellationToken, continuationOptions, scheduler);
255 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
256 Action<Task<TAntecedentResult>> continuationAction)
258 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
261 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
262 Action<Task<TAntecedentResult>> continuationAction,
263 CancellationToken cancellationToken)
265 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
268 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
269 Action<Task<TAntecedentResult>> continuationAction,
270 TaskContinuationOptions continuationOptions)
272 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
275 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
276 Action<Task<TAntecedentResult>> continuationAction,
277 CancellationToken cancellationToken,
278 TaskContinuationOptions continuationOptions,
279 TaskScheduler scheduler)
281 return ContinueWhenAny ((Task[]) tasks,
282 (o) => continuationAction ((Task<TAntecedentResult>)o),
283 cancellationToken, continuationOptions, scheduler);
286 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
288 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
291 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
292 Func<Task, TResult> continuationFunction,
293 CancellationToken cancellationToken)
295 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
298 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
299 Func<Task, TResult> continuationFunction,
300 TaskContinuationOptions continuationOptions)
302 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
305 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
306 Func<Task, TResult> continuationFunction,
307 CancellationToken cancellationToken,
308 TaskContinuationOptions continuationOptions,
309 TaskScheduler scheduler)
311 CheckContinueArguments (tasks, continuationFunction, continuationOptions, scheduler);
313 var cont = Task.WhenAnyCore (tasks).ContinueWith<TResult> (TaskActionInvoker.CreateSelected (continuationFunction), cancellationToken, continuationOptions, scheduler);
318 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
319 Func<Task<TAntecedentResult>, TResult> continuationFunction)
321 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
324 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
325 Func<Task<TAntecedentResult>, TResult> continuationFunction,
326 CancellationToken cancellationToken)
328 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
331 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
332 Func<Task<TAntecedentResult>, TResult> continuationFunction,
333 TaskContinuationOptions continuationOptions)
335 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
338 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
339 Func<Task<TAntecedentResult>, TResult> continuationFunction,
340 CancellationToken cancellationToken,
341 TaskContinuationOptions continuationOptions,
342 TaskScheduler scheduler)
344 return ContinueWhenAny<TResult> ((Task[])tasks,
345 (t) => continuationFunction((Task<TAntecedentResult>)t),
351 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
353 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
356 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
358 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
361 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
362 TaskContinuationOptions continuationOptions)
364 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
367 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
368 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
370 CheckContinueArguments (tasks, continuationAction, continuationOptions, scheduler);
372 var cont = Task.WhenAllCore (tasks).ContinueWith (TaskActionInvoker.Create (continuationAction, tasks), cancellationToken, continuationOptions, scheduler);
377 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
378 Action<Task<TAntecedentResult>[]> continuationAction)
380 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
383 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
384 Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
386 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
389 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
390 TaskContinuationOptions continuationOptions)
392 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
395 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
396 Action<Task<TAntecedentResult>[]> continuationAction,
397 CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
398 TaskScheduler scheduler)
400 return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
401 continuationOptions, scheduler);
404 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
406 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
409 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
410 TaskContinuationOptions continuationOptions)
412 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
415 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
416 CancellationToken cancellationToken)
418 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
421 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
422 CancellationToken cancellationToken,
423 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
425 CheckContinueArguments (tasks, continuationFunction, continuationOptions, scheduler);
427 var cont = Task.WhenAllCore (tasks).ContinueWith<TResult> (TaskActionInvoker.Create (continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);
432 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
433 Func<Task<TAntecedentResult>[], TResult> continuationFunction)
435 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
438 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
439 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
440 TaskContinuationOptions continuationOptions)
442 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
445 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
446 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
447 CancellationToken cancellationToken)
449 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
452 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
453 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
454 CancellationToken cancellationToken,
455 TaskContinuationOptions continuationOptions,
456 TaskScheduler scheduler)
458 return ContinueWhenAll<TResult> ((Task[]) tasks,
459 (o) => continuationFunction (tasks),
461 continuationOptions, scheduler);
466 #region FromAsync IAsyncResult
468 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
470 return FromAsync (asyncResult, endMethod, creationOptions);
473 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions)
475 return FromAsync (asyncResult, endMethod, creationOptions, GetScheduler ());
478 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
480 if (endMethod == null)
481 throw new ArgumentNullException ("endMethod");
483 return TaskFactory<object>.FromIAsyncResult (asyncResult,
485 endMethod (asyncResult);
487 }, creationOptions, scheduler);
490 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
492 return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
495 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
497 return FromAsync<TResult> (asyncResult, endMethod, creationOptions, GetScheduler ());
500 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
502 return TaskFactory<TResult>.FromIAsyncResult (asyncResult, endMethod, creationOptions, scheduler);
507 #region FromAsync Begin/End Method
509 public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod, object state)
511 return FromAsync (beginMethod, endMethod, state, creationOptions);
514 public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
515 object state, TaskCreationOptions creationOptions)
517 return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
518 l => { endMethod (l); return null; },
519 state, creationOptions);
522 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
523 TArg1 arg1, object state)
525 return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
528 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
529 TArg1 arg1, object state, TaskCreationOptions creationOptions)
531 if (endMethod == null)
532 throw new ArgumentNullException ("endMethod");
534 return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
535 l => { endMethod (l); return null; },
536 arg1, state, creationOptions);
539 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
540 Action<IAsyncResult> endMethod,
541 TArg1 arg1, TArg2 arg2, object state)
543 return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
546 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
547 Action<IAsyncResult> endMethod,
548 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
550 if (endMethod == null)
551 throw new ArgumentNullException ("endMethod");
553 return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
554 l => { endMethod (l); return null; },
555 arg1, arg2, state, creationOptions);
558 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
559 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
561 return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
564 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
565 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
567 if (endMethod == null)
568 throw new ArgumentNullException ("endMethod");
570 return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
571 l => { endMethod (l); return null; },
572 arg1, arg2, arg3, state, creationOptions);
575 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
578 return FromAsync (beginMethod, endMethod, state, creationOptions);
581 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
582 object state, TaskCreationOptions creationOptions)
584 return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, state, creationOptions);
587 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
588 TArg1 arg1, object state)
590 return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
593 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
594 TArg1 arg1, object state, TaskCreationOptions creationOptions)
596 return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, state, creationOptions);
599 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
600 Func<IAsyncResult, TResult> endMethod,
601 TArg1 arg1, TArg2 arg2, object state)
603 return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
606 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
607 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
609 return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, state, creationOptions);
612 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
613 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
615 return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
618 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
619 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
621 return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
626 TaskScheduler GetScheduler ()
628 return scheduler ?? TaskScheduler.Current;
631 static void CheckContinueArguments (Task[] tasks, object continuationAction, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
634 throw new ArgumentNullException ("tasks");
636 if (tasks.Length == 0)
637 throw new ArgumentException ("The tasks argument contains no tasks", "tasks");
639 foreach (var ta in tasks) {
641 throw new ArgumentException ("The tasks argument contains a null value", "tasks");
644 if (continuationAction == null)
645 throw new ArgumentNullException ("continuationAction");
646 if (scheduler == null)
647 throw new ArgumentNullException ("scheduler");
649 const TaskContinuationOptions notAllowedOptions =
650 TaskContinuationOptions.NotOnRanToCompletion |
651 TaskContinuationOptions.NotOnFaulted |
652 TaskContinuationOptions.NotOnCanceled |
653 TaskContinuationOptions.OnlyOnRanToCompletion |
654 TaskContinuationOptions.OnlyOnFaulted |
655 TaskContinuationOptions.OnlyOnCanceled;
657 if ((continuationOptions & notAllowedOptions) != 0)
658 throw new ArgumentOutOfRangeException ("continuationOptions");