Esempio n. 1
0
        /// <summary>
        /// Creates a new, completed task for the given result.
        /// </summary>
        /// <typeparam name="T">The result type of the task.</typeparam>
        /// <param name="p_result"></param>
        /// <returns>A completed task.</returns>
        public static UnityTask FromException(Exception p_exception)
        {
            var tcs = new UnityTaskCompletionSource <int>();

            tcs.SetException(p_exception);
            return(tcs.Task);
        }
Esempio n. 2
0
        /// <summary>
        /// 创建 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" />,它是以指定的异常来完成的。
        /// </summary>
        /// <typeparam name="T">任务返回的结果的类型</typeparam>
        /// <param name="exception">完成任务的异常</param>
        /// <returns>出错的任务</returns>
        public static UnityTask <T> FromException <T>(Exception exception)
        {
            var uTcs = new UnityTaskCompletionSource <T>();

            uTcs.SetException(exception);
            return(uTcs.Task);
        }
Esempio 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);
        }
Esempio n. 4
0
        /// <summary>
        ///   创建一个任务,该任务将在可枚举集合中的所有 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 对象都完成时完成。
        /// </summary>
        /// <param name="tasks">等待完成的任务。</param>
        /// <returns>表示所有提供的任务的完成情况的任务。</returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="tasks" /> 参数为 <see langword="null" />。
        /// </exception>
        /// <exception cref="T:System.ArgumentException">
        ///   此 <paramref name="tasks" /> 集合包含 <see langword="null" /> 任务。
        /// </exception>
        public static UnityTask WhenAll(IEnumerable <UnityTask> tasks)
        {
            var taskArray = tasks.ToArray();

            if (taskArray.Length == 0)
            {
                return(FromResult(0));
            }
            var uTcs = new UnityTaskCompletionSource <object>();

            Factory.ContinueWhenAll(taskArray, t =>
            {
                Exception[] exceptions = taskArray.Where(p => p.IsFaulted).Select(p => p.Exception).ToArray();
                if (exceptions.Length > 0)
                {
                    foreach (var e in exceptions)
                    {
                        Debug.LogError(e);
                    }
                    uTcs.SetException(new System.AggregateException(exceptions));
                }
                else if (taskArray.Any(p => p.IsCanceled))
                {
                    uTcs.SetCanceled();
                }
                else
                {
                    uTcs.SetResult(0);
                }
            });
            return(uTcs.Task);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates and starts a Task<TResult>
        /// </summary>
        /// <typeparam name="TArg1"></typeparam>
        /// <param name="p_func"></param>
        /// <returns></returns>
        public UnityTask <TResult> StartNew <TResult>(Func <System.Collections.IEnumerator> p_func)
        {
            var tcs = new UnityTaskCompletionSource <TResult>();

            tcs.Task.TaskGenerator = () => p_func();
            tcs.Task.ReturnResult  = result =>
            {
                try
                {
                    tcs.SetResult((TResult)result);
                }
                catch (Exception e)
                {
                    tcs.SetException(e);
                }
            };
            tcs.Task.TaskCoroutine = UnityTaskScheduler.FromCurrentSynchronizationContext().Post(tcs.Task.TaskGenerator(), tcs.Task.ReturnResult);
            return(tcs.Task);
        }