示例#1
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);
        }
 /// <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());
 }
示例#3
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);
         }
         //}
     }));
 }
示例#4
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());
 }
示例#5
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()));
 }
示例#6
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()));
 }
 /// <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());
 }
 /// <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());
 }