Exemplo n.º 1
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 成功执行时异步执行的延续任务。
        /// </summary>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <returns>
        ///   一个新的延续 <see cref="T:UnityEngine.TaskExtension.UnityTask" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static UnityTask OnSuccess(this UnityTask task, Action <UnityTask> continuation)
        {
            UnityTaskCompletionSource <object> uTcs = new UnityTaskCompletionSource <object>();

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    uTcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    uTcs.TrySetCanceled();
                }
                else
                {
                    try
                    {
                        continuation(t);
                        uTcs.TrySetResult(null);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e);
                        uTcs.TrySetException(e);
                    }
                }
            });
            return(uTcs.Task);
        }
Exemplo n.º 2
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 成功执行时异步执行的延续任务。
        /// </summary>
        /// <typeparam name="TResult">该任务的结果的类型。</typeparam>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask`1" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <returns>
        ///   一个新的延续 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static UnityTask <TResult> OnSuccess <TResult>(this UnityTask task, Func <UnityTask, TResult> continuation)
        {
            UnityTaskCompletionSource <TResult> uTcs = new UnityTaskCompletionSource <TResult>();

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    uTcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    uTcs.TrySetCanceled();
                }
                else
                {
                    try
                    {
                        TResult result = continuation(t);
                        uTcs.TrySetResult(result);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e);
                        uTcs.TrySetException(e);
                    }
                }
            });
            return(uTcs.Task);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a task that is complete when all of the provided tasks are complete.
        /// If any of the tasks has an exception, all exceptions raised in the tasks will
        /// be aggregated into the returned task. Otherwise, if any of the tasks is cancelled,
        /// the returned task will be cancelled.
        /// </summary>
        /// <param name="p_tasks">The tasks to aggregate.</param>
        /// <returns>A task that is complete when all of the provided tasks are complete.</returns>
        public static UnityTask WhenAll(IEnumerable <UnityTask> p_tasks)
        {
            var taskArr = p_tasks.ToArray();

            if (taskArr.Length == 0)
            {
                return(UnityTask.FromResult(0));
            }
            var tcs = new UnityTaskCompletionSource <object>();

            UnityTask.Factory.ContinueWhenAll(taskArr, _ =>
            {
                var exceptions = taskArr.Where(p => p.IsFaulted).Select(p => p.Exception).ToArray();
                if (exceptions.Length > 0)
                {
                    tcs.SetException(new System.Threading.Tasks.AggregateException(exceptions));
                }
                else if (taskArr.Any(t => t.IsCanceled))
                {
                    tcs.SetCanceled();
                }
                else
                {
                    tcs.SetResult(0);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Unwraps a nested task, producing a task that is complete when both the outer
        /// and inner tasks are complete and that has the inner task's result.
        /// This is primarily useful for chaining asynchronous operations together.
        /// </summary>
        /// <param name="task">The task to unwrap.</param>
        /// <returns>A new task that is complete when both the outer and inner tasks
        /// are complete and that has the inner task's result.</returns>
        public static UnityTask <T> Unwrap <T>(this UnityTask <UnityTask <T> > task)
        {
            var tcs = new UnityTaskCompletionSource <T>();

            task.ContinueWith((UnityTask <UnityTask <T> > t) =>
            {
                if (t.IsFaulted)
                {
                    tcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    t.Result.ContinueWith(delegate(UnityTask <T> inner)
                    {
                        if (inner.IsFaulted)
                        {
                            tcs.TrySetException(inner.Exception);
                        }
                        else if (inner.IsCanceled)
                        {
                            tcs.TrySetCanceled();
                        }
                        else
                        {
                            tcs.TrySetResult(inner.Result);
                        }
                    });
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Unwraps a nested task, producing a task that is complete when both the outer and inner tasks are complete. This is primarily useful for chaining asynchronous operations together.
        /// </summary>
        /// <param name="p_task">The task to unwrap.</param>
        /// <returns>A new task that is complete when both the outer and inner tasks are complete.</returns>
        public static UnityTask Unwrap(this UnityTask <UnityTask> p_task)
        {
            var tcs = new UnityTaskCompletionSource <object>();

            p_task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    tcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    p_task.Result.ContinueWith(inner =>
                    {
                        if (inner.IsFaulted)
                        {
                            tcs.TrySetException(inner.Exception);
                        }
                        else if (inner.IsCanceled)
                        {
                            tcs.TrySetCanceled();
                        }
                        else
                        {
                            tcs.TrySetResult(null);
                        }
                    });
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 6
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 成功执行时异步执行的<see cref="T:System.Threading.Tasks.Task`1" />延续任务。
        /// </summary>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask`1" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <param name="cancellationToken">将指派给新的延续任务的 <see cref="P:System.Threading.Tasks.TaskFactory.CancellationToken" />。</param>
        /// <returns>
        ///   一个新的延续 <see cref="T:System.Threading.Tasks.Task" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static Task <TResult> ContinueToBackground <TIn, TResult>(this UnityTask <TIn> task,
                                                                         Func <UnityTask <TIn>, TResult> continuation, CancellationToken cancellationToken)
        {
            TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>();

            task.ContinueWith(p =>
            {
                if (p.IsFaulted || p.IsCanceled)
                {
                    tcs.TrySetException(p.Exception);
                }
                else
                {
                    Task.Run(() => continuation(p), cancellationToken).ContinueWith(t =>
                    {
                        if (t.IsFaulted || t.IsCanceled)
                        {
                            tcs.TrySetException(t.Exception ?? t.DefaultException());
                        }
                        else
                        {
                            tcs.TrySetResult(t.Result);
                        }
                    }, cancellationToken);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 7
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 成功执行时异步执行的<see cref="T:System.Threading.Tasks.Task" />延续任务。
        /// </summary>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <param name="cancellationToken">将指派给新的延续任务的 <see cref="P:System.Threading.Tasks.TaskFactory.CancellationToken" />。</param>
        /// <returns>
        ///   一个新的延续 <see cref="T:System.Threading.Tasks.Task" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static Task ContinueToBackground(this UnityTask task, Action <UnityTask> continuation,
                                                CancellationToken cancellationToken)
        {
            TaskCompletionSource <int> tcs = new TaskCompletionSource <int>();

            task.ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    tcs.TrySetException(t.Exception);
                }
                else
                {
                    Task.Run(() => { continuation(t); }, cancellationToken)
                    .ContinueWith(tt =>
                    {
                        if (tt.IsFaulted || tt.IsCanceled)
                        {
                            tcs.TrySetException(tt.Exception ?? tt.DefaultException());
                        }
                        else
                        {
                            tcs.TrySetResult(0);
                        }
                    }, cancellationToken);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Registers a continuation for the task that will run when the task is complete.
        /// </summary>
        /// <typeparam name="T">The type returned by the continuation.</typeparam>
        /// <param name="p_continuation">The continuation to run after the task completes.
        /// The function takes the completed task as an argument and can return a value.</param>
        /// <param name="p_cancellationToken">The cancellation token.</param>
        /// <returns>A new Task that returns the value returned by the continuation after both
        /// the task and the continuation are complete.</returns>
        public UnityTask <UnityTask> ContinueWith(Func <UnityTask, IEnumerator> p_continuation, System.Threading.Tasks.CancellationToken p_cancellationToken)
        {
            Func <UnityTask, UnityTask> continuation = t =>
            {
                return(UnityTask.Run(() => p_continuation(t)));
            };

            return(ContinueWith(continuation, p_cancellationToken));
        }
        /// <summary>
        /// Attempts to transition the underlying Task<TResult> into the Faulted state and binds it to a specified exception.
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public bool TrySetException(Exception exception)
        {
            System.Threading.Tasks.AggregateException exception2 = exception as System.Threading.Tasks.AggregateException;
            if (exception2 != null)
            {
                return(this.Task.TrySetException(exception2));
            }
            UnityTask <T> task = this.Task;

            Exception[] innerExceptions = new Exception[] { exception };
            return(task.TrySetException(new System.Threading.Tasks.AggregateException(innerExceptions).Flatten()));
        }
Exemplo n.º 10
0
 /// <summary>
 /// 作为后台线程
 /// </summary>
 /// <param name="p_task"></param>
 /// <returns></returns>
 public static UnityTask <T> AsForeground <T>(this Task <T> p_task)
 {
     return(p_task.ContinueToForeground(t =>
     {
         if (t.IsFaulted)
         {
             return UnityTask.FromException <T>(t.Exception);
         }
         else
         {
             return UnityTask.FromResult(t.Result);
         }
     }).Unwrap());
 }
Exemplo n.º 11
0
 /// <summary>
 /// 作为后台线程
 /// </summary>
 /// <param name="p_task"></param>
 /// <returns></returns>
 public static Task AsBackground(this UnityTask p_task)
 {
     return(p_task.ContinueToBackground(t =>
     {
         if (t.IsFaulted)
         {
             return Task.FromException(t.Exception);
         }
         else
         {
             return Task.FromResult(0);
         }
     }).Unwrap());
 }
Exemplo n.º 12
0
        /// <summary>
        /// OnSuccess
        /// </summary>
        /// <param name="p_task"></param>
        /// <param name="p_continuation"></param>
        /// <returns></returns>
        public static UnityTask OnSuccess(this UnityTask p_task, Action <UnityTask> p_continuation)
        {
            UnityTaskCompletionSource <object> utcs = new UnityTaskCompletionSource <object>();

            p_task.ContinueWith(t =>
            {
                if (!t.IsFaulted && !t.IsCanceled)
                {
                    p_continuation(t);
                    utcs.TrySetResult(null);
                }
                else
                {
                    utcs.TrySetException(t.Exception);
                }
            });
            return(utcs.Task);
        }
Exemplo n.º 13
0
        /// <summary>
        /// OnSuccess
        /// </summary>
        /// <param name="p_task"></param>
        /// <param name="p_continuation"></param>
        /// <returns></returns>
        public static UnityTask <TResult> OnSuccess <TResult>(this UnityTask p_task, Func <UnityTask, TResult> p_continuation)
        {
            UnityTaskCompletionSource <TResult> utcs = new UnityTaskCompletionSource <TResult>();

            p_task.ContinueWith(t =>
            {
                if (!t.IsFaulted && !t.IsCanceled)
                {
                    TResult result = p_continuation(t);
                    utcs.TrySetResult(result);
                }
                else
                {
                    utcs.TrySetException(t.Exception);
                }
            });

            return(utcs.Task);
        }
Exemplo n.º 14
0
 /// <summary>
 /// Registers a continuation for the task that will run when the task is complete.
 /// </summary>
 /// <param name="continuation">The continuation to run after the task completes.
 /// The function takes the completed task as an argument.</param>
 /// <returns>A new Task that is complete after both the task and the continuation are
 /// complete.</returns>
 public UnityTask ContinueWith(Action <UnityTask <T> > p_continuation)
 {
     return(base.ContinueWith(t =>
     {
         //if (t.IsFaulted)
         //{
         //    return UnityTask.FromException<int>(t.Exception);
         //}
         //else
         //{
         try
         {
             p_continuation((UnityTask <T>)t);
             return UnityTask.FromResult(0);
         }
         catch (Exception ex)
         {
             return UnityTask.FromException <int>(ex);
         }
         //}
     }));
 }
Exemplo n.º 15
0
 /// <summary>
 /// Registers a continuation for the task that will run when the task is complete.
 /// </summary>
 /// <param name="continuation">The continuation to run after the task completes.
 /// The function takes the completed task as an argument.</param>
 /// <returns>A new Task that is complete after both the task and the continuation are
 /// complete.</returns>
 public UnityTask <T2> ContinueWith <T2>(Func <UnityTask <T>, T2> p_continuation)
 {
     return(base.ContinueWith(t =>
     {
         //if (t.IsFaulted)
         //{
         //    return UnityTask.FromException<T2>(t.Exception);
         //}
         //else
         //{
         try
         {
             T2 result = p_continuation((UnityTask <T>)t);
             return UnityTask.FromResult(result);
         }
         catch (Exception ex)
         {
             return UnityTask.FromException <T2>(ex);
         }
         //}
     }).Unwrap());
 }
Exemplo n.º 16
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 成功执行时异步执行的<see cref="T:System.Threading.Tasks.Task`1" />延续任务。
        /// </summary>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <param name="cancellationToken">将指派给新的延续任务的 <see cref="P:System.Threading.Tasks.TaskFactory.CancellationToken" />。</param>
        /// <returns>
        ///   一个新的延续 <see cref="T:System.Threading.Tasks.Task" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static Task <TResult> ContinueToBackground <TResult>(this UnityTask task,
                                                                    Func <UnityTask, TResult> continuation, CancellationToken cancellationToken)
        {
            TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>();

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    Debug.LogError(t.Exception);
                    tcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    Task.Run(() => continuation(t), cancellationToken).ContinueWith(a =>
                    {
                        if (a.IsFaulted)
                        {
                            Debug.LogError(a.Exception);
                            tcs.TrySetException(a.Exception ?? a.DefaultException());
                        }
                        else if (a.IsCanceled)
                        {
                            tcs.TrySetCanceled();
                        }
                        else
                        {
                            tcs.TrySetResult(a.Result);
                        }
                    }, cancellationToken);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 17
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 成功执行时异步执行的<see cref="T:System.Threading.Tasks.Task" />延续任务。
        /// </summary>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask`1" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <param name="cancellationToken">将指派给新的延续任务的 <see cref="P:System.Threading.Tasks.TaskFactory.CancellationToken" />。</param>
        /// <returns>
        ///   一个新的延续 <see cref="T:System.Threading.Tasks.Task" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static Task ContinueToBackground <T>(this UnityTask <T> task, Action <UnityTask <T> > continuation,
                                                    CancellationToken cancellationToken)
        {
            TaskCompletionSource <object> tcs = new TaskCompletionSource <object>();

            task.ContinueWith(p =>
            {
                if (p.IsFaulted)
                {
                    Debug.LogError(p.Exception);
                    tcs.TrySetException(p.Exception);
                }
                else if (p.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    Task.Run(() => continuation(p), cancellationToken).ContinueWith(t =>
                    {
                        if (t.IsFaulted)
                        {
                            Debug.LogError(t.Exception);
                            tcs.TrySetException(t.Exception ?? t.DefaultException());
                        }
                        else if (t.IsCanceled)
                        {
                            tcs.TrySetCanceled();
                        }
                        else
                        {
                            tcs.TrySetResult(0);
                        }
                    }, cancellationToken);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 18
0
        /// <summary>
        /// switch to backgroud processor, thread pool
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="p_task"></param>
        /// <param name="p_continuation"></param>
        /// <returns></returns>
        public static Task <TResult> ContinueToBackground <T, TResult>(this UnityTask <T> p_task, Func <UnityTask <T>, TResult> p_continuation, CancellationToken p_cancellationToken)
        {
            TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>();
            var cancellation = p_cancellationToken.Register(() => tcs.TrySetCanceled());

            p_task.ContinueWith(t =>
            {
                TaskScheduler.FromCurrentSynchronizationContext().Post(() =>
                {
                    try
                    {
                        tcs.SetResult(p_continuation(t));
                        cancellation.Dispose();
                    }
                    catch (Exception e)
                    {
                        tcs.SetException(e);
                        cancellation.Dispose();
                    }
                });
            });
            return(tcs.Task);
        }
Exemplo n.º 19
0
        /// <summary>
        ///   创建一个代理 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 表示异步操作的Task
        /// </summary>
        /// <param name="task">
        ///   进行解包的任务。
        /// </param>
        /// <typeparam name="T">该任务的结果的类型。</typeparam>
        /// <returns>
        ///   一个 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 表示所提供的异步操作Task
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   如果引发的异常 <paramref name="task" /> 参数为 null。
        /// </exception>
        public static UnityTask <T> Unwrap <T>(this UnityTask <UnityTask <T> > task)
        {
            var uTcs = new UnityTaskCompletionSource <T>();

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    Debug.LogError(t.Exception);
                    uTcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    uTcs.TrySetCanceled();
                }
                else
                {
                    t.Result.ContinueWith(inner =>
                    {
                        if (inner.IsFaulted)
                        {
                            Debug.LogError(inner.Exception);
                            uTcs.TrySetException(inner.Exception);
                        }
                        else if (inner.IsCanceled)
                        {
                            uTcs.TrySetCanceled();
                        }
                        else
                        {
                            uTcs.TrySetResult(inner.Result);
                        }
                    });
                }
            });
            return(uTcs.Task);
        }
Exemplo n.º 20
0
 /// <summary>
 /// 作为后台线程
 /// </summary>
 /// <param name="task">前台线程</param>
 /// <returns></returns>
 public static Task AsBackground(this UnityTask task)
 {
     return(task.ContinueToBackground(p => p.IsFaulted ? Task.FromException(p.Exception) : Task.FromResult(0))
            .Unwrap());
 }
 /// <summary>
 ///   创建一个 <see cref="T:UnityEngine.TaskExtension.UnityTaskCompletionSource`1" />。
 /// </summary>
 public UnityTaskCompletionSource()
 {
     Task = new UnityTask <TResult>();
 }
Exemplo n.º 22
0
 /// <summary>
 /// Executes a function asynchronously, returning a task that represents the operation.
 /// </summary>
 /// <typeparam name="T">The return type of the task.</typeparam>
 /// <param name="p_toRun">The function to run.</param>
 /// <returns>A task representing the asynchronous operation.</returns>
 public static UnityTask <T> Run <T>(Func <T> p_action)
 {
     return(UnityTask.FromResult(0).ContinueWith(t => p_action()));
 }
Exemplo n.º 23
0
 /// <summary>
 /// Executes a function asynchronously, returning a task that represents the operation.
 /// </summary>
 /// <typeparam name="T">The return type of the task.</typeparam>
 /// <param name="p_toRun">The function to run.</param>
 /// <returns>A task representing the asynchronous operation.</returns>
 public static UnityTask Run(Action p_action)
 {
     return(UnityTask.FromResult(0).ContinueWith(t => p_action()));
 }
Exemplo n.º 24
0
        /// <summary>
        /// Registers a continuation for the task that will run when the task is complete.
        /// </summary>
        /// <typeparam name="T">The type returned by the continuation.</typeparam>
        /// <param name="p_continuation">The continuation to run after the task completes.
        /// The function takes the completed task as an argument and can return a value.</param>
        /// <param name="p_cancellationToken">The cancellation token.</param>
        /// <returns>A new Task that returns the value returned by the continuation after both
        /// the task and the continuation are complete.</returns>
        public UnityTask <UnityTask> ContinueWith(Func <UnityTask, UnityTask> p_continuation, System.Threading.Tasks.CancellationToken p_cancellationToken)
        {
            bool completed = false;

            UnityTaskCompletionSource <UnityTask> utcs        = new UnityTaskCompletionSource <UnityTask>();
            CancellationTokenRegistration         cancelToken = p_cancellationToken.Register(() => utcs.TrySetCanceled());

            // 此处防止判断为false之后正好执行完毕
            completed = IsCompleted;
            if (!completed)
            {
                m_continuationActions.Add(t =>
                {
                    //if (t.IsFaulted)
                    //{
                    //    utcs.TrySetException(t.Exception);
                    //    cancelToken.Dispose();
                    //}
                    //else
                    //{
                    try
                    {
                        UnityTask result = p_continuation(t);
                        utcs.TrySetResult(result);
                        cancelToken.Dispose();
                    }
                    catch (Exception ex)
                    {
                        utcs.TrySetException(ex);
                        cancelToken.Dispose();
                    }
                    //}
                });
            }
            else
            {
                ForegroundInvoker.Invoke(() =>
                {
                    //if (this.IsFaulted)
                    //{
                    //    utcs.TrySetException(this.Exception);
                    //    cancelToken.Dispose();
                    //}
                    //else
                    //{
                    try
                    {
                        UnityTask result = p_continuation(this);
                        utcs.TrySetResult(result);
                        cancelToken.Dispose();
                    }
                    catch (Exception ex)
                    {
                        utcs.TrySetException(ex);
                        cancelToken.Dispose();
                    }
                    //}
                });
            }

            return(utcs.Task);
        }
 /// <summary>
 /// Creates a TaskCompletionSource<TResult>.
 /// </summary>
 public UnityTaskCompletionSource()
 {
     this.Task = new UnityTask <T>();
 }
Exemplo n.º 26
0
 /// <summary>
 ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 成功执行时异步执行的<see cref="T:System.Threading.Tasks.Task" />延续任务。
 /// </summary>
 /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask`1" /></param>
 /// <param name="continuation">
 ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 完成时要运行的操作。
 ///    在运行时,委托将作为一个参数传递给完成的任务。
 /// </param>
 /// <returns>
 ///   一个新的延续 <see cref="T:System.Threading.Tasks.Task" />。
 /// </returns>
 /// <exception cref="T:System.ArgumentNullException">
 ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
 /// </exception>
 public static Task ContinueToBackground <T>(this UnityTask <T> task, Action <UnityTask <T> > continuation)
 {
     return(task.ContinueToBackground(continuation, CancellationToken.None));
 }
Exemplo n.º 27
0
 /// <summary>
 /// 作为Unity主线程
 /// </summary>
 /// <param name="task">一个<see cref="T:System.Threading.Tasks.Task`1" />任务</param>
 /// <returns></returns>
 public static UnityTask <TResult> AsForeground <TResult>(this Task <TResult> task)
 {
     return(task.ContinueToForeground(p =>
                                      p.IsFaulted ? UnityTask.FromException <TResult>(p.Exception) : UnityTask.FromResult(p.Result)).Unwrap());
 }
Exemplo n.º 28
0
 /// <summary>
 /// 作为Unity主线程
 /// </summary>
 /// <param name="task">一个<see cref="T:System.Threading.Tasks.Task" />任务</param>
 /// <returns></returns>
 public static UnityTask AsForeground(this Task task)
 {
     return(task.ContinueToForeground(p =>
                                      p.IsFaulted ? UnityTask.FromException <int>(p.Exception) : UnityTask.FromResult(0)).Unwrap());
 }
Exemplo n.º 29
0
 /// <summary>
 /// 作为后台线程
 /// </summary>
 /// <param name="task">前台线程</param>
 /// <returns></returns>
 public static Task <T> AsBackground <T>(this UnityTask <T> task)
 {
     return(task.ContinueToBackground(p =>
                                      p.IsFaulted ? Task.FromException <T>(p.Exception) : Task.FromResult(p.Result)).Unwrap());
 }
Exemplo n.º 30
0
 /// <summary>
 ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 成功执行时异步执行的<see cref="T:System.Threading.Tasks.Task`1" />延续任务。
 /// </summary>
 /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask`1" /></param>
 /// <param name="continuation">
 ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 完成时要运行的操作。
 ///    在运行时,委托将作为一个参数传递给完成的任务。
 /// </param>
 /// <returns>
 ///   一个新的延续 <see cref="T:System.Threading.Tasks.Task" />。
 /// </returns>
 /// <exception cref="T:System.ArgumentNullException">
 ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
 /// </exception>
 public static Task <TResult> ContinueToBackground <TIn, TResult>(this UnityTask <TIn> task,
                                                                  Func <UnityTask <TIn>, TResult> continuation)
 {
     return(task.ContinueToBackground(continuation, CancellationToken.None));
 }