internal static Task <TResult> FromAsyncImpl <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) { AsyncCallback callback = null; if (beginMethod == null) { throw new ArgumentNullException("beginMethod"); } if (endMethod == null) { throw new ArgumentNullException("endMethod"); } TaskFactory.CheckFromAsyncOptions(creationOptions, true); TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>(state, creationOptions); try { if (callback == null) { callback = delegate(IAsyncResult iar) { TaskFactory <TResult> .FromAsyncCoreLogic(iar, this.endMethod, this.tcs); }; } beginMethod(arg1, arg2, arg3, callback, state); } catch { tcs.TrySetResult(default(TResult)); throw; } return(tcs.Task); }
internal static Task <TResult> FromAsyncImpl(IAsyncResult asyncResult, Func <IAsyncResult, TResult> endMethod, TaskCreationOptions creationOptions, TaskScheduler scheduler, ref StackCrawlMark stackMark) { WaitOrTimerCallback callBack = null; if (asyncResult == null) { throw new ArgumentNullException("asyncResult"); } if (endMethod == null) { throw new ArgumentNullException("endMethod"); } if (scheduler == null) { throw new ArgumentNullException("scheduler"); } TaskFactory.CheckFromAsyncOptions(creationOptions, false); TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>(creationOptions); Task t = new Task(delegate { TaskFactory <TResult> .FromAsyncCoreLogic(asyncResult, endMethod, tcs); }, null, Task.InternalCurrent, System.Threading.CancellationToken.None, TaskCreationOptions.None, InternalTaskOptions.None, null, ref stackMark); if (asyncResult.IsCompleted) { try { t.RunSynchronously(scheduler); } catch (Exception exception) { tcs.TrySetException(exception); } } else { if (callBack == null) { callBack = delegate { try { t.RunSynchronously(scheduler); } catch (Exception exception) { tcs.TrySetException(exception); } }; } ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, callBack, null, -1, true); } return(tcs.Task); }