Esempio n. 1
0
        /// <summary>
        /// Creates a Task that represents the completion of another Task, and
        /// that schedules an AsyncCallback to run upon completion.
        /// </summary>
        /// <param name="task">The antecedent Task.</param>
        /// <param name="callback">The AsyncCallback to run.</param>
        /// <param name="state">The object state to use with the AsyncCallback.</param>
        /// <returns>The new task.</returns>
        public static Task <TResult> ToAsync <TResult>(this Task <TResult> task, AsyncCallback callback, object state)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }

            var tcs = new TaskCompletionSource <TResult>(state);

            task.ContinueWith(_ =>
            {
                tcs.SetFromTask(task);
                callback?.Invoke(tcs.Task);
            });
            return(tcs.Task);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a Task that represents the completion of another Task, and
        /// that schedules an AsyncCallback to run upon completion.
        /// </summary>
        /// <param name="task">The antecedent Task.</param>
        /// <param name="callback">The AsyncCallback to run.</param>
        /// <param name="state">The object state to use with the AsyncCallback.</param>
        /// <returns>The new task.</returns>
        public static Task ToAsync(this Task task, AsyncCallback callback, object state)
        {
            Contract.Requires(task != null);

            var tcs = new TaskCompletionSource <object>(state);

            task.ContinueWith(_ =>
            {
                tcs.SetFromTask(task);
                if (callback != null)
                {
                    callback(tcs.Task);
                }
            });
            return(tcs.Task);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a Task that represents the completion of another Task, and
        /// that schedules an AsyncCallback to run upon completion.
        /// </summary>
        /// <param name="task">The antecedent Task.</param>
        /// <param name="callback">The AsyncCallback to run.</param>
        /// <param name="state">The object state to use with the AsyncCallback.</param>
        /// <returns>The new task.</returns>
        public static Task <TResult> ToAsync <TResult>(this Task <TResult> task, AsyncCallback callback, object state)
        {
            if (task == null)
            {
                throw new ArgumentNullException("task");
            }
            TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>(state);

            task.ContinueWith(delegate(Task <TResult> _)
            {
                tcs.SetFromTask(task);
                if (callback != null)
                {
                    callback(tcs.Task);
                }
            });
            return(tcs.Task);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a Task that represents the completion of another Task, and
        /// that schedules an AsyncCallback to run upon completion.
        /// </summary>
        /// <param name="task">The antecedent Task.</param>
        /// <param name="callback">The AsyncCallback to run.</param>
        /// <param name="state">The object state to use with the AsyncCallback.</param>
        /// <returns>The new task.</returns>
        public static Task ToAsync(this Task task, AsyncCallback callback, object state)
        {
            if (task == null)
            {
                throw new ArgumentNullException("task");
            }

            var tcs = new TaskCompletionSource <object>(state);

            task.ContinueWith(_ =>
            {
                tcs.SetFromTask(task);
                if (callback != null)
                {
                    callback(tcs.Task);
                }
            });
            return(tcs.Task);
        }
 /// <summary>Transfers the result of a Task to the TaskCompletionSource.</summary>
 /// <typeparam name="TResult">Specifies the type of the result.</typeparam>
 /// <param name="resultSetter">The TaskCompletionSource.</param>
 /// <param name="task">The task whose completion results should be transfered.</param>
 public static void SetFromTask <TResult>(this TaskCompletionSource <TResult> resultSetter, Task <TResult> task)
 {
     resultSetter.SetFromTask <TResult>((Task)task);
 }