Merge pull request #167 from konrad-kruczynski/send_async_fix
[mono.git] / mcs / class / corlib / System.Threading.Tasks / TaskFactory.cs
index 495dd830843dcb1b929874eb3552d200f627fe22..03d5dd1244f3864300362404e3af783f00c8d570 100644 (file)
@@ -1,10 +1,12 @@
 // 
 // 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
@@ -34,19 +36,18 @@ namespace System.Threading.Tasks
        
        public class TaskFactory
        {
-               TaskScheduler scheduler;
+               readonly TaskScheduler scheduler;
                TaskCreationOptions creationOptions;
                TaskContinuationOptions continuationOptions;
                CancellationToken cancellationToken;
                
-               #region ctors
                public TaskFactory ()
-                       : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
+                       : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, null)
                {
                }
 
                public TaskFactory (CancellationToken cancellationToken)
-                       : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
+                       : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, null)
                {       
                }
 
@@ -56,7 +57,7 @@ namespace System.Threading.Tasks
                }
                
                public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
-                       : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
+                       : this (CancellationToken.None, creationOptions, continuationOptions, null)
                {       
                }
                
@@ -67,43 +68,81 @@ namespace System.Threading.Tasks
                        this.scheduler = scheduler;
                        this.creationOptions = creationOptions;
                        this.continuationOptions = continuationOptions;
+
+                       CheckContinuationOptions (continuationOptions);
+               }
+               
+               public TaskScheduler Scheduler {
+                       get {
+                               return scheduler;
+                       }
+               }
+               
+               public TaskContinuationOptions ContinuationOptions {
+                       get {
+                               return continuationOptions;
+                       }
+               }
+               
+               public TaskCreationOptions CreationOptions {
+                       get {
+                               return creationOptions;
+                       }
+               }
+               
+               public CancellationToken CancellationToken {
+                       get {
+                               return cancellationToken;
+                       }
+               }
+
+               internal static void CheckContinuationOptions (TaskContinuationOptions continuationOptions)
+               {
+                       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");
                }
-               #endregion
                
                #region StartNew for Task
                public Task StartNew (Action action)
                {
-                       return StartNew (action, cancellationToken, creationOptions, scheduler);
+                       return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task StartNew (Action action, CancellationToken cancellationToken)
                {
-                       return StartNew (action, cancellationToken, creationOptions, scheduler);
+                       return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task StartNew (Action action, TaskCreationOptions creationOptions)
                {
-                       return StartNew (action, cancellationToken, creationOptions, scheduler);
+                       return StartNew (action, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task StartNew (Action<object> action, object state)
                {
-                       return StartNew (action, state, cancellationToken, creationOptions, scheduler);
+                       return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken)
                {
-                       return StartNew (action, state, cancellationToken, creationOptions, scheduler);
+                       return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task StartNew (Action<object> action, object state, TaskCreationOptions creationOptions)
                {
-                       return StartNew (action, state, cancellationToken, creationOptions, scheduler);
+                       return StartNew (action, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task StartNew (Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
                {
-                       return StartNew ((o) => action (), null, cancellationToken, creationOptions, scheduler);
+                       Task t = new Task (action, cancellationToken, creationOptions);
+                       t.Start (scheduler);
+
+                       return t;
                }
                
                public Task StartNew (Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions,
@@ -119,18 +158,18 @@ namespace System.Threading.Tasks
                #region StartNew for Task<TResult>      
                public Task<TResult> StartNew<TResult> (Func<TResult> function)
                {
-                       return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
+                       return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task<TResult> StartNew<TResult> (Func<TResult> function, TaskCreationOptions creationOptions)
                {
-                       return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
+                       return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
 
                }
                
                public Task<TResult> StartNew<TResult> (Func<TResult> function, CancellationToken cancellationToken)
                {
-                       return StartNew<TResult> (function, cancellationToken, creationOptions, scheduler);
+                       return StartNew<TResult> (function, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task<TResult> StartNew<TResult> (Func<TResult> function,
@@ -143,17 +182,17 @@ namespace System.Threading.Tasks
                
                public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state)
                {
-                       return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
+                       return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, CancellationToken cancellationToken)
                {
-                       return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
+                       return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
                {
-                       return StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
+                       return StartNew<TResult> (function, state, cancellationToken, creationOptions, GetScheduler ());
                }
                
                public Task<TResult> StartNew<TResult> (Func<object, TResult> function, object state,
@@ -172,17 +211,17 @@ namespace System.Threading.Tasks
                
                public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction)
                {
-                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, CancellationToken cancellationToken)
                {
-                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task ContinueWhenAny (Task[] tasks, Action<Task> continuationAction, TaskContinuationOptions continuationOptions)
                {
-                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
 
                public Task ContinueWhenAny (Task[] tasks,
@@ -193,35 +232,36 @@ namespace System.Threading.Tasks
                {
                        var ourTasks = (Task[])tasks.Clone ();
                        AtomicBoolean trigger = new AtomicBoolean ();
-                       Task commonContinuation = new Task (null);
+                       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.ContinueWithCore (commonContinuation, TaskContinuationOptions.None, scheduler);
+                               cont.ContinueWith (continuationFunc);
                        }
                        
-                       return commonContinuation;
+                       return commonContinuation.Task;
                }
                
                public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
                                                                Action<Task<TAntecedentResult>> continuationAction)
                {
-                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
                                                                Action<Task<TAntecedentResult>> continuationAction,
                                                                CancellationToken cancellationToken)
                {
-                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
                                                                Action<Task<TAntecedentResult>> continuationAction,
                                                                TaskContinuationOptions continuationOptions)
                {
-                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
 
                public Task ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
@@ -237,21 +277,21 @@ namespace System.Threading.Tasks
 
                public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks, Func<Task, TResult> continuationFunction)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
 
                public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
                                                               Func<Task, TResult> continuationFunction,
                                                               CancellationToken cancellationToken)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
 
                public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
                                                               Func<Task, TResult> continuationFunction,
                                                               TaskContinuationOptions continuationOptions)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
 
                public Task<TResult> ContinueWhenAny<TResult> (Task[] tasks,
@@ -275,21 +315,21 @@ namespace System.Threading.Tasks
                public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
                                                                                  Func<Task<TAntecedentResult>, TResult> continuationFunction)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
 
                public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
                                                                                  Func<Task<TAntecedentResult>, TResult> continuationFunction,
                                                                                  CancellationToken cancellationToken)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
 
                public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
                                                                                  Func<Task<TAntecedentResult>, TResult> continuationFunction,
                                                                                  TaskContinuationOptions continuationOptions)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
 
                public Task<TResult> ContinueWhenAny<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
@@ -307,18 +347,18 @@ namespace System.Threading.Tasks
                
                public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction)
                {
-                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken)
                {
-                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction,
                                             TaskContinuationOptions continuationOptions)
                {
-                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task ContinueWhenAll (Task[] tasks, Action<Task[]> continuationAction, CancellationToken cancellationToken,
@@ -337,19 +377,19 @@ namespace System.Threading.Tasks
                public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
                                                                Action<Task<TAntecedentResult>[]> continuationAction)
                {
-                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
                                                                Action<Task<TAntecedentResult>[]> continuationAction, CancellationToken cancellationToken)
                {
-                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, Action<Task<TAntecedentResult>[]> continuationAction,
                                                                TaskContinuationOptions continuationOptions)
                {
-                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll (tasks, continuationAction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks, 
@@ -363,19 +403,19 @@ namespace System.Threading.Tasks
                
                public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction)
                {
-                       return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
                                                               TaskContinuationOptions continuationOptions)
                {
-                       return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
                                                               CancellationToken cancellationToken)
                {
-                       return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll<TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task<TResult> ContinueWhenAll<TResult> (Task[] tasks, Func<Task[], TResult> continuationFunction,
@@ -395,21 +435,21 @@ namespace System.Threading.Tasks
                public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
                                                                                  Func<Task<TAntecedentResult>[], TResult> continuationFunction)
                {
-                       return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
                                                                                  Func<Task<TAntecedentResult>[], TResult> continuationFunction,
                                                                                  TaskContinuationOptions continuationOptions)
                {
-                       return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks,
                                                                                  Func<Task<TAntecedentResult>[], TResult> continuationFunction,
                                                                                  CancellationToken cancellationToken)
                {
-                       return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return ContinueWhenAll<TAntecedentResult, TResult> (tasks, continuationFunction, cancellationToken, continuationOptions, GetScheduler ());
                }
                
                public Task<TResult> ContinueWhenAll<TAntecedentResult, TResult> (Task<TAntecedentResult>[] tasks, 
@@ -425,523 +465,170 @@ namespace System.Threading.Tasks
                }
 
                #endregion
+
+               #region FromAsync IAsyncResult
                
-               #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)
                {
                        return FromAsync (asyncResult, endMethod, creationOptions);
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
-                                      TaskCreationOptions creationOptions)
+               public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions)
                {
-                       return FromAsync (asyncResult, endMethod, creationOptions);
+                       return FromAsync (asyncResult, endMethod, creationOptions, GetScheduler ());
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod,
-                                      TaskCreationOptions creationOptions, TaskScheduler scheduler)
+
+               public Task FromAsync (IAsyncResult asyncResult, Action<IAsyncResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
                {
-                       throw new NotSupportedException (errorMsg);
+                       if (endMethod == null)
+                               throw new ArgumentNullException ("endMethod");
+
+                       return TaskFactory<object>.FromIAsyncResult (asyncResult,
+                               l => {
+                                       endMethod (asyncResult);
+                                       return null;
+                               }, creationOptions, scheduler);
                }
                
-               [MonoLimitation(errorMsg)]
                public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod)
                {
                        return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
-                                                        TaskCreationOptions creationOptions)
+               public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions)
                {
-                       return FromAsync<TResult> (asyncResult, endMethod, creationOptions);
+                       return FromAsync<TResult> (asyncResult, endMethod, creationOptions, GetScheduler ());
                }
                
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
-                                                        TaskCreationOptions creationOptions, TaskScheduler scheduler)
+               public Task<TResult> FromAsync<TResult> (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return TaskFactory<TResult>.FromIAsyncResult (asyncResult, endMethod, creationOptions, scheduler);
                }
-               
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                      object state)
+
+               #endregion
+
+               #region FromAsync Begin/End Method
+
+               public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod, object state)
                {
-                       return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
+                       return FromAsync (beginMethod, endMethod, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                      object state, TaskCreationOptions creationOptions)
+
+               public Task FromAsync (Func<AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+                                                          object state, TaskCreationOptions creationOptions)
                {
-                       return FromAsync<object> ((a, c, o) => beginMethod (c, o), endMethod, state, creationOptions);
+                       return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
+                               l => { endMethod (l); return null; },
+                               state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
-                                             TArg1 arg1, object state)
+
+               public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+                                                 TArg1 arg1, object state)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+
+               public Task FromAsync<TArg1> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
                                              TArg1 arg1, 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, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+
+               public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
+                                                    Action<IAsyncResult> endMethod,
                                                     TArg1 arg1, TArg2 arg2, object state)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+
+               public Task FromAsync<TArg1, TArg2> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
+                                                    Action<IAsyncResult> endMethod,
                                                     TArg1 arg1, TArg2 arg2, 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, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+
+               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)
                {
-                       throw new NotSupportedException (errorMsg);
+                       return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
                }
-               
-               [MonoLimitation(errorMsg)]
-               public Task FromAsync<TArg1, TArg2, TArg3> (Func<TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult> beginMethod, Action<IAsyncResult> endMethod,
+
+               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)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                        Func<IAsyncResult, TResult> endMethod,
-                                      object state, TaskCreationOptions creationOptions)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                               Func<IAsyncResult, TResult> endMethod,
-                                                               TArg1 arg1, object state)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [MonoLimitation(errorMsg)]
-               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);
-               }
-               
-               [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)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [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)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [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)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [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)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               #endregion
-               
-               public TaskScheduler Scheduler {
-                       get {
-                               return scheduler;
-                       }
-               }
-               
-               public TaskContinuationOptions ContinuationOptions {
-                       get {
-                               return continuationOptions;
-                       }
-               }
-               
-               public TaskCreationOptions CreationOptions {
-                       get {
-                               return creationOptions;
-                       }
-               }
-               
-               public CancellationToken CancellationToken {
-                       get {
-                               return cancellationToken;
-                       }
-               }
-       }
-       
-       public class TaskFactory<TResult>
-       {
-               TaskScheduler scheduler;
-               TaskCreationOptions creationOptions;
-               TaskContinuationOptions continuationOptions;
-               CancellationToken cancellationToken;
-               
-               TaskFactory parent;
-               
-               #region ctors
-               public TaskFactory ()
-                       : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
-               {       
-               }
-               
-               public TaskFactory (TaskScheduler scheduler)
-                       : this (CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, scheduler)
-               {       
-               }
-               
-               public TaskFactory (CancellationToken cancellationToken)
-                       : this (cancellationToken, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Current)
-               {       
-               }
-               
-               public TaskFactory (TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions)
-                       : this (CancellationToken.None, creationOptions, continuationOptions, TaskScheduler.Current)
-               {       
-               }
-               
-               public TaskFactory (CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskContinuationOptions continuationOptions,
-                                   TaskScheduler scheduler)
-               {
-                       this.cancellationToken = cancellationToken;
-                       this.scheduler = scheduler;
-                       this.creationOptions = creationOptions;
-                       this.continuationOptions = continuationOptions;
-                       
-                       this.parent = new TaskFactory (cancellationToken, creationOptions, continuationOptions, scheduler);
-               }
-               
-               #endregion
-               
-               #region StartNew for Task<TResult>      
-               public Task<TResult> StartNew (Func<TResult> function)
-               {
-                       return StartNew (function, cancellationToken, creationOptions, scheduler);
-               }
-               
-               public Task<TResult> StartNew (Func<TResult> function, TaskCreationOptions creationOptions)
-               {
-                       return StartNew (function, cancellationToken, creationOptions, scheduler);
-               }
-               
-               public Task<TResult> StartNew (Func<TResult> function, CancellationToken cancellationToken)
-               {
-                       return StartNew (function, cancellationToken, creationOptions, scheduler);
-               }
-               
-               public Task<TResult> StartNew (Func<TResult> function, 
-                                              CancellationToken cancellationToken,
-                                              TaskCreationOptions creationOptions,
-                                              TaskScheduler scheduler)
-               {
-                       return StartNew ((o) => function (), null, cancellationToken, creationOptions, scheduler);
-               }
-               
-               public Task<TResult> StartNew (Func<object, TResult> function, object state)
-               {
-                       return StartNew (function, state, cancellationToken, creationOptions, scheduler);
-               }
-               
-               public Task<TResult> StartNew (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
-               {
-                       return StartNew (function, state, cancellationToken, creationOptions, scheduler);
-               }
-               
-               public Task<TResult> StartNew (Func<object, TResult> function, object state, CancellationToken cancellationToken)
-               {
-                       return StartNew (function, state, cancellationToken, creationOptions, scheduler);
-               }
-               
-               public Task<TResult> StartNew (Func<object, TResult> function, object state, 
-                                              CancellationToken cancellationToken,
-                                              TaskCreationOptions creationOptions,
-                                              TaskScheduler scheduler)
-               {
-                       return parent.StartNew<TResult> (function, state, cancellationToken, creationOptions, scheduler);
-               }
-               #endregion
-               
-               #region Continue
+                       if (endMethod == null)
+                               throw new ArgumentNullException ("endMethod");
 
-               public Task<TResult> ContinueWhenAny (Task[] tasks,
-                                                     Func<Task, TResult> continuationFunction)
-               {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return TaskFactory<object>.FromAsyncBeginEnd (beginMethod,
+                               l => { endMethod (l); return null; },
+                               arg1, arg2, arg3, state, creationOptions);
                }
 
-               public Task<TResult> ContinueWhenAny (Task[] tasks,
-                                                     Func<Task, TResult> continuationFunction,
-                                                     CancellationToken cancellationToken)
+               public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                        object state)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return FromAsync (beginMethod, endMethod, state, creationOptions);
                }
 
-               public Task<TResult> ContinueWhenAny (Task[] tasks,
-                                                     Func<Task, TResult> continuationFunction,
-                                                     TaskContinuationOptions continuationOptions)
+               public Task<TResult> FromAsync<TResult> (Func<AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                        object state, TaskCreationOptions creationOptions)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, state, creationOptions);
                }
 
-               public Task<TResult> ContinueWhenAny (Task[] tasks,
-                                                     Func<Task, TResult> continuationFunction,
-                                                     CancellationToken cancellationToken,
-                                                     TaskContinuationOptions continuationOptions,
-                                                     TaskScheduler scheduler)
+               public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                               TArg1 arg1, object state)
                {
-                       return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return FromAsync (beginMethod, endMethod, arg1, state, creationOptions);
                }
 
-               public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
-                                                                        Func<Task<TAntecedentResult>, TResult> continuationFunction)
+               public Task<TResult> FromAsync<TArg1, TResult> (Func<TArg1, AsyncCallback, object, IAsyncResult> beginMethod, Func<IAsyncResult, TResult> endMethod,
+                                                               TArg1 arg1, object state, TaskCreationOptions creationOptions)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, state, creationOptions);
                }
 
-               public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
-                                                                        Func<Task<TAntecedentResult>, TResult> continuationFunction,
-                                                                        CancellationToken cancellationToken)
+               public Task<TResult> FromAsync<TArg1, TArg2, TResult> (Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginMethod,
+                                                                      Func<IAsyncResult, TResult> endMethod,
+                                                                      TArg1 arg1, TArg2 arg2, object state)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return FromAsync (beginMethod, endMethod, arg1, arg2, state, creationOptions);
                }
 
-               public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
-                                                                        Func<Task<TAntecedentResult>, TResult> continuationFunction,
-                                                                        TaskContinuationOptions continuationOptions)
+               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)
                {
-                       return ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, state, creationOptions);
                }
 
-               public Task<TResult> ContinueWhenAny<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
-                                                                        Func<Task<TAntecedentResult>, TResult> continuationFunction,
-                                                                        CancellationToken cancellationToken,
-                                                                        TaskContinuationOptions continuationOptions,
-                                                                        TaskScheduler scheduler)
-               {
-                       return parent.ContinueWhenAny (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
-               }
-               
-               public Task<TResult> ContinueWhenAll (Task[] tasks,
-                                                     Func<Task[], TResult> continuationFunction)
-               {
-                       return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
-               }
-               
-               public Task<TResult> ContinueWhenAll (Task[] tasks,
-                                                     Func<Task[], TResult> continuationFunction,
-                                                     TaskContinuationOptions continuationOptions)
-               {
-                       return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
-               }
-               
-               public Task<TResult> ContinueWhenAll (Task[] tasks,
-                                                     Func<Task[], TResult> continuationFunction,
-                                                     CancellationToken cancellationToken)
-               {
-                       return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
-               }
-               
-               public Task<TResult> ContinueWhenAll (Task[] tasks,
-                                                     Func<Task[], TResult> continuationFunction,
-                                                     CancellationToken cancellationToken,
-                                                     TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
-               {
-                       return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
-               }
-               
-               public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
-                                                                        Func<Task<TAntecedentResult>[], TResult> continuationFunction)
-               {
-                       return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
-               }
-               
-               public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
-                                                                        Func<Task<TAntecedentResult>[], TResult> continuationFunction,
-                                                                        TaskContinuationOptions continuationOptions)
-               {
-                       return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
-               }
-               
-               public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
-                                                                        Func<Task<TAntecedentResult>[], TResult> continuationFunction,
-                                                                        CancellationToken cancellationToken)
+               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)
                {
-                       return ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return FromAsync (beginMethod, endMethod, arg1, arg2, arg3, state, creationOptions);
                }
-               
-               public Task<TResult> ContinueWhenAll<TAntecedentResult> (Task<TAntecedentResult>[] tasks,
-                                                                        Func<Task<TAntecedentResult>[], TResult> continuationFunction,
-                                                                        CancellationToken cancellationToken,
-                                                                        TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
+
+               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)
                {
-                       return parent.ContinueWhenAll (tasks, continuationFunction, cancellationToken, continuationOptions, scheduler);
+                       return TaskFactory<TResult>.FromAsyncBeginEnd (beginMethod, endMethod, arg1, arg2, arg3, 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)
-               {
-                       return FromAsync (asyncResult, endMethod, creationOptions);
-               }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
-                                               TaskCreationOptions creationOptions)
-               {
-                       return FromAsync (asyncResult, endMethod, creationOptions);
-               }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync (IAsyncResult asyncResult, Func<IAsyncResult, TResult> endMethod,
-                                               TaskCreationOptions creationOptions, TaskScheduler scheduler)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
-                                               Func<IAsyncResult, TResult> endMethod,
-                                               object state)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync (Func<AsyncCallback, Object, IAsyncResult> beginMethod,
-                                               Func<IAsyncResult, TResult> endMethod,
-                                               object state, TaskCreationOptions creationOptions)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                      Func<IAsyncResult, TResult> endMethod,
-                                                      TArg1 arg1, object state)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [MonoLimitation(errorMsg)]
-               public Task<TResult> FromAsync<TArg1> (Func<TArg1, AsyncCallback, Object, IAsyncResult> beginMethod,
-                                                      Func<IAsyncResult, TResult> endMethod,
-                                                      TArg1 arg1, object state, TaskCreationOptions creationOptions)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [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)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [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)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [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)
-               {
-                       throw new NotSupportedException (errorMsg);
-               }
-               
-               [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)
+
+               TaskScheduler GetScheduler ()
                {
-                       throw new NotSupportedException (errorMsg);
-               }
-               #endregion
-               
-               public TaskScheduler Scheduler {
-                       get {
-                               return scheduler;
-                       }
-               }
-               
-               public TaskContinuationOptions ContinuationOptions {
-                       get {
-                               return continuationOptions;
-                       }
-               }
-               
-               public TaskCreationOptions CreationOptions {
-                       get {
-                               return creationOptions;
-                       }
-               }
-               
-               public CancellationToken CancellationToken {
-                       get {
-                               return cancellationToken;
-                       }
+                       return scheduler ?? TaskScheduler.Current;
                }
        }
 }