Merge pull request #167 from konrad-kruczynski/send_async_fix
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskFactory.cs
index c15389f00a45b222237c3d8eda18f1696adb361d..03d5dd1244f3864300362404e3af783f00c8d570 100644 (file)
@@ -1,11 +1,12 @@
-#if NET_4_0
 // 
 // TaskFactory.cs
 //  
-// Author:
+// Authors:
 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
+//       Marek Safar <marek.safar@gmail.com>
 // 
 // Copyright (c) 2009 Jérémie "Garuma" Laval
+// Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
 // 
 // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to deal
@@ -25,6 +26,8 @@
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 
+#if NET_4_0 || MOBILE
+
 using System;
 using System.Threading;
 
@@ -33,647 +36,599 @@ namespace System.Threading.Tasks
        
        public class TaskFactory
        {
-               TaskScheduler scheduler;
-               TaskCreationOptions options;
-               TaskContinuationOptions contOptions;            
+               readonly TaskScheduler scheduler;
+               TaskCreationOptions creationOptions;
+               TaskContinuationOptions continuationOptions;
+               CancellationToken cancellationToken;
                
-               #region ctors
-               public TaskFactory () : this (TaskScheduler.Current, TaskCreationOptions.None, TaskContinuationOptions.None)
+               public TaskFactory ()
+                       : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, null)
+               {
+               }
+
+               public TaskFactory (CancellationToken cancellationToken)
+                       : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
                {       
                }
-               
-               public TaskFactory (TaskScheduler scheduler) : this (scheduler, TaskCreationOptions.None, TaskContinuationOptions.None)
+
+               public TaskFactory (TaskScheduler scheduler)
+                       : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
                {       
                }
                
-               public TaskFactory (TaskCreationOptions options, TaskContinuationOptions contOptions)
-                       : this (TaskScheduler.Current, options, contOptions)
+               public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
+                       : this (CancellationToken.None, creationOptions, continuationOptions, null)
                {       
                }
                
-               public TaskFactory (TaskScheduler scheduler, TaskCreationOptions options, TaskContinuationOptions contOptions)
+               public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
+                                   TaskScheduler scheduler)
                {
+                       this.cancellationToken = cancellationToken;
                        this.scheduler = scheduler;
-                       this.options = options;
-                       this.contOptions = contOptions;
+                       this.creationOptions = creationOptions;
+                       this.continuationOptions = continuationOptions;
+
+                       CheckContinuationOptions (continuationOptions);
                }
-               #endregion
                
-               #region StartNew for Task
-               public Task StartNew (Action action)
-               {
-                       return StartNew (action, options, scheduler);
+               public TaskScheduler Scheduler {
+                       get {
+                               return scheduler;
+                       }
                }
                
-               public Task StartNew (Action action, TaskCreationOptions options)
-               {
-                       return StartNew (action, options, scheduler);
+               public TaskContinuationOptions ContinuationOptions {
+                       get {
+                               return continuationOptions;
+                       }
                }
                
-               public Task StartNew (Action action, TaskCreationOptions options, TaskScheduler scheduler)
-               {
-                       return StartNew ((o) => action (), null, options, scheduler);
+               public TaskCreationOptions CreationOptions {
+                       get {
+                               return creationOptions;
+                       }
                }
                
-               public Task StartNew (Action<object> action, object state)
+               public CancellationToken CancellationToken {
+                       get {
+                               return cancellationToken;
+                       }
+               }
+
+               internal static void CheckContinuationOptions (TaskContinuationOptions continuationOptions)
                {
-                       return StartNew (action, state, options, scheduler);
+                       if ((continuationOptions & (TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.NotOnRanToCompletion)) != 0)
+                               throw new ArgumentOutOfRangeException ("continuationOptions");
+
+                       const TaskContinuationOptions long_running = TaskContinuationOptions.LongRunning | TaskContinuationOptions.ExecuteSynchronously;
+                       if ((continuationOptions & long_running) == long_running)
+                               throw new ArgumentOutOfRangeException ("continuationOptions", "Synchronous continuations cannot be long running");
                }
                
-               public Task StartNew (Action<object> action, object state, TaskCreationOptions options)
+               #region StartNew for Task
+               public Task StartNew (Action action)
                {
-                       return StartNew (action, state, options, scheduler);
+                       return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
                }
                
-               public Task StartNew (Action<object> action, object state, TaskCreationOptions options, TaskScheduler scheduler)
+               public Task StartNew (Action action, CancellationToken cancellationToken)
                {
-                       Task t = new Task (action, state, options);
-                       t.Start (scheduler);
-                       
-                       return t;
+                       return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
                }
-               #endregion
                
-               #region StartNew for Task<TResult>      
-               public Task<TResult> StartNew<TResult> (Func<TResult> function)
+               public Task StartNew (Action action, TaskCreationOptions creationOptions)
                {
-                       return StartNew<TResult> (function, options, scheduler);
+                       return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
                }
                
-               public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions options)
+               public Task StartNew (Action<object> action, object state)
                {
-                       return StartNew<TResult> (function, options, scheduler);
+                       return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
-               public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions options, TaskScheduler scheduler)
+               public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken)
                {
-                       return StartNew<TResult> ((o) => function (), null, options, scheduler);
+                       return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
-               public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
+               public Task StartNew (Action<object> action, object state, TaskCreationOptions creationOptions)
                {
-                       return StartNew<TResult> (function, state, options, scheduler);
+                       return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
-               public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions options)
+               public Task StartNew (Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
                {
-                       return StartNew<TResult> (function, state, options, scheduler);
+                       Task t = new Task (action, cancellationToken, creationOptions);
+                       t.Start (scheduler);
+
+                       return t;
                }
                
-               public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions options,
-                                                       TaskScheduler scheduler)
+               public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
+                                     TaskScheduler scheduler)
                {
-                       Task<TResult> t = new Task<TResult> (function, state, options);
+                       Task t = new Task (action, state, cancellationToken, creationOptions);
                        t.Start (scheduler);
                        
                        return t;
                }
                #endregion
                
-               #region Continue
-               
-               [MonoTODO]
-               public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
+               #region StartNew for Task<TResult>      
+               public Task<TResult> StartNew<TResult> (Func<TResult> function)
                {
-                       return ContinueWhenAny (tasks, continuationAction, contOptions, scheduler);
+                       return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
                }
                
-               [MonoTODO]
-               public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
+               public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
                {
-                       return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
-               }
+                       return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
 
-               [MonoTODO]
-               public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions,
-                                           TaskScheduler scheduler)
-               {
-                 return null;
                }
                
-               [MonoTODO]
-               public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction)
+               public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
                {
-                       return ContinueWhenAny (tasks, continuationAction, contOptions);
+                       return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
                }
                
-               [MonoTODO]
-               public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
-                                                     TaskContinuationOptions continuationOptions)
+               public Task<TResult> StartNew<TResult> (Func<TResult> function,
+                                                       CancellationToken cancellationToken,
+                                                       TaskCreationOptions creationOptions,
+                                                       TaskScheduler scheduler)
                {
-                       return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
+                       return StartNew<TResult> ((o) => function (), null, cancellationToken, creationOptions, scheduler);
                }
-
-               [MonoTODO]
-               public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationAction,
-                                                              TaskContinuationOptions continuationOptions,
-                                                              TaskScheduler scheduler)
+               
+               public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
                {
-                       return null;
+                       return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
-               public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction)
+               public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
                {
-                       return ContinueWhenAll (tasks, continuationFunction, contOptions);
+                       return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
-               public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction,
-                                            TaskContinuationOptions continuationOptions)
+               public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
                {
-                       return ContinueWhenAll (tasks, continuationFunction, continuationOptions, scheduler);
+                       return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
-               public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationFunction,
-                                            TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
+               public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
+                                                       CancellationToken cancellationToken,
+                                                       TaskCreationOptions creationOptions,
+                                                       TaskScheduler scheduler)
                {
-                       CountdownEvent evt = new CountdownEvent (tasks.Length);
-                       Task cont = new Task ((o) => continuationFunction ((Task[])o), tasks, options);
-                       
-                       foreach (Task t in tasks)
-                               t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
+                       Task<TResult> t = new Task<TResult> (function, state, cancellationToken, creationOptions);
+                       t.Start (scheduler);
                        
-                       return cont;
+                       return t;
                }
-
+               #endregion
                
-               public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
+               #region Continue
+               
+               public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
                {
-                       return ContinueWhenAll<TResult> (tasks, continuationFunction, contOptions);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
-                                                              TaskContinuationOptions continuationOptions)
+               public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
                {
-                       return ContinueWhenAll<TResult> (tasks, continuationFunction, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
-                                                              TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
+               public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
                {
-                       CountdownEvent evt = new CountdownEvent (tasks.Length);
-                       Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), tasks, options);
-                       
-                       foreach (Task t in tasks)
-                               t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
-                       
-                       return cont;
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
 
-               #endregion
-               
-               #region FromAsync
-               // For these methods to work we first have to convert the ThreadPool to use Tasks as it
-               // is doing in 4.0, then all that is remaining is to identify the Task on which is 
-               // run the async operation (probably with some additional state in a IAsyncResult subclass)
-               // and call its ContinueWith method accordingly
-               
-               const string errorMsg = "Mono's thread pool doesn't support this operation yet";
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
+               public Task ContinueWhenAny (Task[] tasks,
+                                            Action<Task> continuationAction,
+                                            CancellationToken cancellationToken,
+                                            TaskContinuationOptions continuationOptions,
+                                            TaskScheduler scheduler)
                {
-                       return FromAsync (asyncResult, endMethod, options);
+                       var ourTasks = (Task[])tasks.Clone ();
+                       AtomicBoolean trigger = new AtomicBoolean ();
+                       var commonContinuation = new TaskCompletionSource<object> ();
+                       Action<Task> continuationFunc = t => commonContinuation.SetResult (null);
+                       
+                       foreach (Task t in ourTasks) {
+                               Task cont = new Task ((o) => continuationAction ((Task)o), t, cancellationToken, creationOptions, t);
+                               t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
+                               cont.ContinueWith (continuationFunc);
+                       }
+                       
+                       return commonContinuation.Task;
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
-                                      TaskCreationOptions creationOptions)
+               public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
+                                                               Action<Task<TAntecedentResult>> continuationAction)
                {
-                       return FromAsync (asyncResult, endMethod, creationOptions);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
-                                      TaskCreationOptions creationOptions, TaskScheduler scheduler)
+               public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
+                                                               Action<Task<TAntecedentResult>> continuationAction,
+                                                               CancellationToken cancellationToken)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
+               public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
+                                                               Action<Task<TAntecedentResult>> continuationAction,
+                                                               TaskContinuationOptions continuationOptions)
                {
-                       return FromAsync<TResult> (asyncResult, endMethod, options);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
-                                                        TaskCreationOptions creationOptions)
+
+               public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
+                                                               Action<Task<TAntecedentResult>> continuationAction,
+                                                               CancellationToken cancellationToken,
+                                                               TaskContinuationOptions continuationOptions,
+                                                               TaskScheduler scheduler)
                {
-                       return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
+                       return ContinueWhenAny ((Task[]) tasks,
+                                               (o) => continuationAction ((Task<TAntecedentResult>)o),
+                                               cancellationToken, continuationOptions, scheduler);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
-                                                        TaskCreationOptions creationOptions, TaskScheduler scheduler)
+
+               public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
-               
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                      object state)
+
+               public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
+                                                              Func<Task, TResult> continuationFunction,
+                                                              CancellationToken cancellationToken)
                {
-                       return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, options);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                      object state, TaskCreationOptions creationOptions)
+
+               public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
+                                                              Func<Task, TResult> continuationFunction,
+                                                              TaskContinuationOptions continuationOptions)
                {
-                       return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                             TArg1 arg1, object state)
+
+               public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
+                                                              Func<Task, TResult> continuationFunction,
+                                                              CancellationToken cancellationToken,
+                                                              TaskContinuationOptions continuationOptions,
+                                                              TaskScheduler scheduler)
                {
-                       throw new NotSupportedException (errorMsg);
+                       var ourTasks = (Task[])tasks.Clone ();
+                       AtomicBoolean trigger = new AtomicBoolean ();
+                       TaskCompletionSource<TResult> source = new TaskCompletionSource<TResult> ();
+
+                       foreach (Task t in ourTasks) {
+                               Task cont = new Task ((o) => source.SetResult (continuationFunction ((Task)o)), t, cancellationToken, creationOptions, t);
+                               t.ContinueWithCore (cont, continuationOptions, scheduler, trigger.TrySet);
+                       }
+
+                       return source.Task;
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                             TArg1 arg1, object state, TaskCreationOptions creationOptions)
+
+               public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
+                                                                                 Func<Task<TAntecedentResult>, TResult> continuationFunction)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                                    TArg1 arg1, TArg2 arg2, object state)
+
+               public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
+                                                                                 Func<Task<TAntecedentResult>, TResult> continuationFunction,
+                                                                                 CancellationToken cancellationToken)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                                    TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
+
+               public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
+                                                                                 Func<Task<TAntecedentResult>, TResult> continuationFunction,
+                                                                                 TaskContinuationOptions continuationOptions)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                                           TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
+
+               public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
+                                                                                 Func<Task<TAntecedentResult>, TResult> continuationFunction,
+                                                                                 CancellationToken cancellationToken,
+                                                                                 TaskContinuationOptions continuationOptions,
+                                                                                 TaskScheduler scheduler)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAny<TResult> ((Task[])tasks,
+                                                        (t) => continuationFunction((Task<TAntecedentResult>)t),
+                                                        cancellationToken,
+                                                        continuationOptions,
+                                                        scheduler);
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                                           TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }               
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                        Func<IAsyncResult, TResult> endMethod,
-                                                        object state)
+               public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                        Func<IAsyncResult, TResult> endMethod,
-                                      object state, TaskCreationOptions creationOptions)
+               public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                               Func<IAsyncResult, TResult> endMethod,
-                                                               TArg1 arg1, object state)
+               public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
+                                            TaskContinuationOptions continuationOptions)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                            Func<IAsyncResult, TResult> endMethod,
-                                                            TArg1 arg1, object state, TaskCreationOptions creationOptions)
+               public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
+                                            TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
                {
-                       throw new NotSupportedException (errorMsg);
+                       var ourTasks = (Task[])tasks.Clone ();
+                       CountdownEvent evt = new CountdownEvent (ourTasks.Length);
+                       Task cont = new Task ((o) => continuationAction ((Task[])o), ourTasks, cancellationToken, creationOptions);
+                       
+                       foreach (Task t in ourTasks)
+                               t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
+                       
+                       return cont;
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                                      Func<IAsyncResult, TResult> endMethod,
-                                                                      TArg1 arg1, TArg2 arg2, object state)
+               public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
+                                                               Action<Task<TAntecedentResult>[]> continuationAction)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                                      Func<IAsyncResult, TResult> endMethod,
-                                                                      TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
+               public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
+                                                               Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                                             Func<IAsyncResult, TResult> endMethod,
-                                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
+               public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
+                                                               TaskContinuationOptions continuationOptions)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                                             Func<IAsyncResult, TResult> endMethod,
-                                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
-                                                                             TaskCreationOptions creationOptions)
+               public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, 
+                                                               Action<Task<TAntecedentResult>[]> continuationAction,
+                                                               CancellationToken cancellationToken, TaskContinuationOptions continuationOptions,
+                                                               TaskScheduler scheduler)
                {
-                       throw new NotSupportedException (errorMsg);
-               }
-               #endregion
-               
-               public TaskScheduler Scheduler {
-                       get {
-                               return scheduler;
-                       }
-               }
-               
-               public TaskContinuationOptions ContinuationOptions {
-                       get {
-                               return contOptions;
-                       }
+                       return ContinueWhenAll ((Task[]) tasks, (o) => continuationAction (tasks), cancellationToken,
+                                               continuationOptions, scheduler);
                }
                
-               public TaskCreationOptions CreationOptions {
-                       get {
-                               return options;
-                       }
-               }
-       }
-       
-       public class TaskFactory<TResult>
-       {
-               TaskScheduler scheduler;
-               TaskCreationOptions options;
-               TaskContinuationOptions contOptions;
-               
-               TaskFactory parent;
-               
-               #region ctors
-               public TaskFactory () : this (TaskScheduler.Current, TaskCreationOptions.None, TaskContinuationOptions.None)
-               {       
-               }
-               
-               public TaskFactory (TaskScheduler scheduler) : this (scheduler, TaskCreationOptions.None, TaskContinuationOptions.None)
-               {       
-               }
-               
-               public TaskFactory (TaskCreationOptions options, TaskContinuationOptions contOptions)
-                       : this (TaskScheduler.Current, options, contOptions)
-               {       
+               public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
+               {
+                       return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               public TaskFactory (TaskScheduler scheduler, TaskCreationOptions options, TaskContinuationOptions contOptions)
+               public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
+                                                              TaskContinuationOptions continuationOptions)
                {
-                       this.scheduler = scheduler;
-                       this.options = options;
-                       this.contOptions = contOptions;
-                       this.parent = new TaskFactory (scheduler, options, contOptions);
+                       return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
-               #endregion
                
-               #region StartNew for Task<TResult>      
-               public Task<TResult> StartNew (Func<TResult> function)
+               public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
+                                                              CancellationToken cancellationToken)
                {
-                       return StartNew (function, options, scheduler);
+                       return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions options)
+               public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
+                                                              CancellationToken cancellationToken,
+                                                              TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
                {
-                       return StartNew (function, options, scheduler);
+                       var ourTasks = (Task[])tasks.Clone ();
+                       CountdownEvent evt = new CountdownEvent (ourTasks.Length);
+                       Task<TResult> cont = new Task<TResult> ((o) => continuationFunction ((Task[])o), ourTasks, cancellationToken, creationOptions);
+                       
+                       foreach (Task t in ourTasks)
+                               t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
+                       
+                       return cont;
                }
                
-               public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions options, TaskScheduler scheduler)
+               public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
+                                                                                 Func<Task<TAntecedentResult>[], TResult> continuationFunction)
                {
-                       return StartNew ((o) => function (), null, options, scheduler);
+                       return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               public Task<TResult> StartNew (Func<object, TResult> function, object state)
+               public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
+                                                                                 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
+                                                                                 TaskContinuationOptions continuationOptions)
                {
-                       return StartNew (function, state, options, scheduler);
+                       return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions options)
+               public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
+                                                                                 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
+                                                                                 CancellationToken cancellationToken)
                {
-                       return StartNew (function, state, options, scheduler);
+                       return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
-               public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions options,
-                                                       TaskScheduler scheduler)
+               public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
+                                                                                 Func<Task<TAntecedentResult>[], TResult> continuationFunction,
+                                                                                 CancellationToken cancellationToken,
+                                                                                 TaskContinuationOptions continuationOptions,
+                                                                                 TaskScheduler scheduler)
                {
-                       return parent.StartNew<TResult> (function, state, options, scheduler);
+                       return ContinueWhenAll<TResult> ((Task[]) tasks,
+                                                        (o) => continuationFunction (tasks),
+                                                        cancellationToken,
+                                                        continuationOptions, scheduler);
                }
+
                #endregion
+
+               #region FromAsync IAsyncResult
                
-               #region Continue
-               [MonoTODO]
-               public Task ContinueWhenAny (Task<TResult>[] tasks, Action<Task<TResult>> continuationAction)
+               public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod)
                {
-                       return ContinueWhenAny (tasks, continuationAction, contOptions, scheduler);
+                       return FromAsync (asyncResult, endMethod, creationOptions);
                }
                
-               [MonoTODO]
-               public Task ContinueWhenAny (Task<TResult>[] tasks, Action<Task<TResult>> continuationAction,
-                                            TaskContinuationOptions continuationOptions)
+               public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions)
                {
-                       return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
+                       return FromAsync (asyncResult, endMethod, creationOptions, GetScheduler ());
                }
 
-               [MonoTODO]
-               public Task ContinueWhenAny (Task<TResult>[] tasks, Action<Task<TResult>> continuationAction,
-                                            TaskContinuationOptions continuationOptions,
-                                            TaskScheduler scheduler)
+               public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
                {
-                 return null;
+                       if (endMethod == null)
+                               throw new ArgumentNullException ("endMethod");
+
+                       return TaskFactory<object>.FromIAsyncResult (asyncResult,
+                               l => {
+                                       endMethod (asyncResult);
+                                       return null;
+                               }, creationOptions, scheduler);
                }
                
-               [MonoTODO]
-               public Task<TNewResult> ContinueWhenAny<TNewResult> (Task<TResult>[] tasks, Func<Task, TNewResult> continuationAction)
+               public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
                {
-                       return ContinueWhenAny (tasks, continuationAction, contOptions);
+                       return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
                }
                
-               [MonoTODO]
-               public Task<TNewResult> ContinueWhenAny<TNewResult> (Task<TResult>[] tasks, Func<Task, TNewResult> continuationAction,
-                                                     TaskContinuationOptions continuationOptions)
+               public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
                {
-                       return ContinueWhenAny (tasks, continuationAction, continuationOptions, scheduler);
-               }
-
-               [MonoTODO]
-               public Task<TNewResult> ContinueWhenAny<TNewResult> (Task<TResult>[] tasks, Func<Task, TNewResult> continuationAction,
-                                                              TaskContinuationOptions continuationOptions,
-                                                              TaskScheduler scheduler)
-               {
-                       return null;
+                       return FromAsync<TResult> (asyncResult, endMethod, creationOptions, GetScheduler ());
                }
                
-               public Task ContinueWhenAll (Task<TResult>[] tasks, Action<Task<TResult>[]> continuationFunction)
+               public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
                {
-                       return ContinueWhenAll (tasks, continuationFunction, contOptions);
+                       return TaskFactory<TResult>.FromIAsyncResult (asyncResult, endMethod, creationOptions, scheduler);
                }
-               
-               public Task ContinueWhenAll (Task<TResult>[] tasks, Action<Task<TResult>[]> continuationFunction,
-                                            TaskContinuationOptions continuationOptions)
+
+               #endregion
+
+               #region FromAsync Begin/End Method
+
+               public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod, object state)
                {
-                       return ContinueWhenAll (tasks, continuationFunction, continuationOptions, scheduler);
+                       return FromAsync (beginMethod, endMethod, state, creationOptions);
                }
-               
-               public Task ContinueWhenAll (Task<TResult>[] tasks, Action<Task<TResult>[]> continuationFunction,
-                                            TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
+
+               public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+                                                          object state, TaskCreationOptions creationOptions)
                {
-                       CountdownEvent evt = new CountdownEvent (tasks.Length);
-                       Task cont = new Task ((o) => continuationFunction ((Task<TResult>[])o), tasks, options);
-                       
-                       foreach (Task t in tasks)
-                               t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
-                       
-                       return cont;
+                       return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
+                               l => { endMethod (l); return null; },
+                               state, creationOptions);
                }
-               
-               public Task<TNewResult> ContinueWhenAll<TNewResult> (Task<TResult>[] tasks,
-                                                                    Func<Task<TNewResult>[], TNewResult> continuationFunction)
+
+               public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+                                                 TArg1 arg1, object state)
                {
-                       return ContinueWhenAll (tasks, continuationFunction, contOptions);
+                       return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
                }
-               
-               public Task<TNewResult> ContinueWhenAll<TNewResult> (Task<TResult>[] tasks,
-                                                                    Func<Task<TNewResult>[], TNewResult> continuationFunction,
-                                                                    TaskContinuationOptions continuationOptions)
+
+               public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+                                             TArg1 arg1, object state, TaskCreationOptions creationOptions)
                {
-                       return ContinueWhenAll (tasks, continuationFunction, continuationOptions, scheduler);
+                       if (endMethod == null)
+                               throw new ArgumentNullException ("endMethod");
+
+                       return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
+                               l => { endMethod (l); return null; },
+                               arg1, state, creationOptions);
                }
-               
-               public Task<TNewResult> ContinueWhenAll<TNewResult> (Task<TResult>[] tasks,
-                                                                    Func<Task<TNewResult>[], TNewResult> continuationFunction,
-                                                                    TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
+
+               public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
+                                                    Action<IAsyncResult> endMethod,
+                                                    TArg1 arg1, TArg2 arg2, object state)
                {
-                       CountdownEvent evt = new CountdownEvent (tasks.Length);
-                       Task<TNewResult> cont = new Task<TNewResult> ((o) => continuationFunction ((Task<TResult>[])o), tasks, options);
-                       
-                       foreach (Task t in tasks)
-                               t.ContinueWithCore (cont, continuationOptions, scheduler, evt.Signal);
-                       
-                       return cont;
+                       return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
                }
 
-               #endregion
-               
-               #region FromAsync
-               const string errorMsg = "Mono's thread pool doesn't support this operation yet";
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
+               public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
+                                                    Action<IAsyncResult> endMethod,
+                                                    TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
                {
-                       return FromAsync (asyncResult, endMethod, options);
+                       if (endMethod == null)
+                               throw new ArgumentNullException ("endMethod");
+
+                       return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
+                               l => { endMethod (l); return null; },
+                               arg1, arg2, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
-                                               TaskCreationOptions creationOptions)
+
+               public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+                                                           TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
                {
-                       return FromAsync (asyncResult, endMethod, creationOptions);
+                       return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
-                                               TaskCreationOptions creationOptions, TaskScheduler scheduler)
+
+               public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+                                                           TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
                {
-                       throw new NotSupportedException (errorMsg);
+                       if (endMethod == null)
+                               throw new ArgumentNullException ("endMethod");
+
+                       return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
+                               l => { endMethod (l); return null; },
+                               arg1, arg2, arg3, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
-                                               Func<IAsyncResult, TResult> endMethod,
-                                               object state)
+
+               public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                        object state)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return FromAsync (beginMethod, endMethod, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
-                                               Func<IAsyncResult, TResult> endMethod,
-                                               object state, TaskCreationOptions creationOptions)
+
+               public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                        object state, TaskCreationOptions creationOptions)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                      Func<IAsyncResult, TResult> endMethod,
-                                                      TArg1 arg1, object state)
+
+               public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                               TArg1 arg1, object state)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                      Func<IAsyncResult, TResult> endMethod,
-                                                      TArg1 arg1, object state, TaskCreationOptions creationOptions)
+
+               public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                               TArg1 arg1, object state, TaskCreationOptions creationOptions)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                             Func<IAsyncResult, TResult> endMethod,
-                                                             TArg1 arg1, TArg2 arg2, object state)
+
+               public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
+                                                                      Func<IAsyncResult, TResult> endMethod,
+                                                                      TArg1 arg1, TArg2 arg2, object state)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                             Func<IAsyncResult, TResult> endMethod,
-                                                             TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
+
+               public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                                      TArg1 arg1, TArg2 arg2, object state, TaskCreationOptions creationOptions)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                                    Func<IAsyncResult, TResult> endMethod,
-                                                                    TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
+
+               public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                                    Func<IAsyncResult, TResult> endMethod,
-                                                                    TArg1 arg1, TArg2 arg2, TArg3 arg3, object state,
-                                                                    TaskCreationOptions creationOptions)
+
+               public Task<TResult> FromAsync<TArg1, TArg2, TArg3, TResult> (Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                                             TArg1 arg1, TArg2 arg2, TArg3 arg3, object state, TaskCreationOptions creationOptions)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
                }
+
                #endregion
-               
-               public TaskScheduler Scheduler {
-                       get {
-                               return scheduler;
-                       }
-               }
-               
-               public TaskContinuationOptions ContinuationOptions {
-                       get {
-                               return contOptions;
-                       }
-               }
-               
-               public TaskCreationOptions CreationOptions {
-                       get {
-                               return options;
-                       }
+
+               TaskScheduler GetScheduler ()
+               {
+                       return scheduler ?? TaskScheduler.Current;
                }
        }
 }