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 Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
226 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
228 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
231 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
233 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
236 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
238 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
241 public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
243 CheckContinueArguments (tasks, continuationAction, continuationOptions, scheduler);
245 var cont = Task.WhenAnyCore (tasks).ContinueWith (TaskActionInvoker.CreateSelected (continuationAction), cancellationToken, continuationOptions, scheduler);
250 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
251 Action<Task<TAntecedentResult>> continuationAction)
253 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
256 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
257 Action<Task<TAntecedentResult>> continuationAction,
258 CancellationToken cancellationToken)
260 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
263 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
264 Action<Task<TAntecedentResult>> continuationAction,
265 TaskContinuationOptions continuationOptions)
267 return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
270 public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
271 Action<Task<TAntecedentResult>> continuationAction,
272 CancellationToken cancellationToken,
273 TaskContinuationOptions continuationOptions,
274 TaskScheduler scheduler)
276 return ContinueWhenAny ((Task[]) tasks,
277 (o) => continuationAction ((Task<TAntecedentResult>)o),
278 cancellationToken, continuationOptions, scheduler);
281 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
283 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
286 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
287 Func<Task, TResult> continuationFunction,
288 CancellationToken cancellationToken)
290 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
293 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
294 Func<Task, TResult> continuationFunction,
295 TaskContinuationOptions continuationOptions)
297 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
300 public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
301 Func<Task, TResult> continuationFunction,
302 CancellationToken cancellationToken,
303 TaskContinuationOptions continuationOptions,
304 TaskScheduler scheduler)
306 CheckContinueArguments (tasks, continuationFunction, continuationOptions, scheduler);
308 var cont = Task.WhenAnyCore (tasks).ContinueWith<TResult> (TaskActionInvoker.Create (continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);
313 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
314 Func<Task<TAntecedentResult>, TResult> continuationFunction)
316 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
319 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
320 Func<Task<TAntecedentResult>, TResult> continuationFunction,
321 CancellationToken cancellationToken)
323 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
326 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
327 Func<Task<TAntecedentResult>, TResult> continuationFunction,
328 TaskContinuationOptions continuationOptions)
330 return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
333 public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
334 Func<Task<TAntecedentResult>, TResult> continuationFunction,
335 CancellationToken cancellationToken,
336 TaskContinuationOptions continuationOptions,
337 TaskScheduler scheduler)
339 return ContinueWhenAny<TResult> ((Task[])tasks,
340 (t) => continuationFunction((Task<TAntecedentResult>)t),
346 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
348 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
351 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
353 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
356 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
357 TaskContinuationOptions continuationOptions)
359 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
362 public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
363 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
365 CheckContinueArguments (tasks, continuationAction, continuationOptions, scheduler);
367 var cont = Task.WhenAllCore (tasks).ContinueWith (TaskActionInvoker.Create (continuationAction, tasks), cancellationToken, continuationOptions, scheduler);
372 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
373 Action<Task<TAntecedentResult>[]> continuationAction)
375 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
378 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
379 Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
381 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
384 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
385 TaskContinuationOptions continuationOptions)
387 return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
390 public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
391 Action<Task<TAntecedentResult>[]> continuationAction,
392 CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
393 TaskScheduler scheduler)
395 return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
396 continuationOptions, scheduler);
399 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
401 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
404 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
405 TaskContinuationOptions continuationOptions)
407 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
410 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
411 CancellationToken cancellationToken)
413 return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
416 public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
417 CancellationToken cancellationToken,
418 TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
420 CheckContinueArguments (tasks, continuationFunction, continuationOptions, scheduler);
422 var cont = Task.WhenAllCore (tasks).ContinueWith<TResult> (TaskActionInvoker.Create (continuationFunction, tasks), cancellationToken, continuationOptions, scheduler);
427 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
428 Func<Task<TAntecedentResult>[], TResult> continuationFunction)
430 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
433 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
434 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
435 TaskContinuationOptions continuationOptions)
437 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
440 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
441 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
442 CancellationToken cancellationToken)
444 return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
447 public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
448 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
449 CancellationToken cancellationToken,
450 TaskContinuationOptions continuationOptions,
451 TaskScheduler scheduler)
453 return ContinueWhenAll<TResult> ((Task[]) tasks,
454 (o) => continuationFunction (tasks),
456 continuationOptions, scheduler);
461 #region FromAsync IAsyncResult
463 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
465 return FromAsync (asyncResult, endMethod, creationOptions);
468 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions)
470 return FromAsync (asyncResult, endMethod, creationOptions, GetScheduler ());
473 public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
475 if (endMethod == null)
476 throw new ArgumentNullException ("endMethod");
478 return TaskFactory<object>.FromIAsyncResult (asyncResult,
480 endMethod (asyncResult);
482 }, creationOptions, scheduler);
485 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
487 return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
490 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
492 return FromAsync<TResult> (asyncResult, endMethod, creationOptions, GetScheduler ());
495 public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
497 return TaskFactory<TResult>.FromIAsyncResult (asyncResult, endMethod, creationOptions, scheduler);
502 #region FromAsync Begin/End Method
504 public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod, object state)
506 return FromAsync (beginMethod, endMethod, state, creationOptions);
509 public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
510 object state, TaskCreationOptions creationOptions)
512 return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
513 l => { endMethod (l); return null; },
514 state, creationOptions);
517 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
518 TArg1 arg1, object state)
520 return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
523 public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
524 TArg1 arg1, object state, TaskCreationOptions creationOptions)
526 if (endMethod == null)
527 throw new ArgumentNullException ("endMethod");
529 return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
530 l => { endMethod (l); return null; },
531 arg1, state, creationOptions);
534 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
535 Action<IAsyncResult> endMethod,
536 TArg1 arg1, TArg2 arg2, object state)
538 return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
541 public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
542 Action<IAsyncResult> endMethod,
543 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
545 if (endMethod == null)
546 throw new ArgumentNullException ("endMethod");
548 return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
549 l => { endMethod (l); return null; },
550 arg1, arg2, state, creationOptions);
553 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
554 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
556 return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
559 public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
560 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
562 if (endMethod == null)
563 throw new ArgumentNullException ("endMethod");
565 return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
566 l => { endMethod (l); return null; },
567 arg1, arg2, arg3, state, creationOptions);
570 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
573 return FromAsync (beginMethod, endMethod, state, creationOptions);
576 public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
577 object state, TaskCreationOptions creationOptions)
579 return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, state, creationOptions);
582 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
583 TArg1 arg1, object state)
585 return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
588 public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
589 TArg1 arg1, object state, TaskCreationOptions creationOptions)
591 return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, state, creationOptions);
594 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
595 Func<IAsyncResult, TResult> endMethod,
596 TArg1 arg1, TArg2 arg2, object state)
598 return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
601 public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
602 TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
604 return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, state, creationOptions);
607 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
608 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
610 return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
613 public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
614 TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
616 return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
621 TaskScheduler GetScheduler ()
623 return scheduler ?? TaskScheduler.Current;
626 static void CheckContinueArguments (Task[] tasks, object continuationAction, TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
629 throw new ArgumentNullException ("tasks");
631 if (tasks.Length == 0)
632 throw new ArgumentException ("The tasks argument contains no tasks", "tasks");
634 foreach (var ta in tasks) {
636 throw new ArgumentException ("The tasks argument contains a null value", "tasks");
639 if (continuationAction == null)
640 throw new ArgumentNullException ("continuationAction");
641 if (scheduler == null)
642 throw new ArgumentNullException ("scheduler");
644 const TaskContinuationOptions notAllowedOptions =
645 TaskContinuationOptions.NotOnRanToCompletion |
646 TaskContinuationOptions.NotOnFaulted |
647 TaskContinuationOptions.NotOnCanceled |
648 TaskContinuationOptions.OnlyOnRanToCompletion |
649 TaskContinuationOptions.OnlyOnFaulted |
650 TaskContinuationOptions.OnlyOnCanceled;
652 if ((continuationOptions & notAllowedOptions) != 0)
653 throw new ArgumentOutOfRangeException ("continuationOptions");