public static async ETTask <bool> WaitAll(List <ETTask> tasks, ETCancellationToken cancellationToken = null)
        {
            if (tasks.Count == 0)
            {
                return(false);
            }

            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Count + 1);

            foreach (ETTask task in tasks)
            {
                RunOneTask(task).Coroutine();
            }

            await coroutineBlocker.WaitAsync();

            async ETVoid RunOneTask(ETTask task)
            {
                await task;
                await coroutineBlocker.WaitAsync();
            }

            if (cancellationToken == null)
            {
                return(true);
            }

            return(!cancellationToken.IsCancel());
        }
        public static async ETTask <bool> WaitAny(ETTask[] tasks, ETCancellationToken cancellationToken = null)
        {
            if (tasks.Length == 0)
            {
                return(false);
            }

            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(2);

            foreach (ETTask task in tasks)
            {
                RunOneTask(task).Coroutine();
            }

            async ETVoid RunOneTask(ETTask task)
            {
                await task;
                await coroutineBlocker.WaitAsync();
            }

            await coroutineBlocker.WaitAsync();

            if (cancellationToken == null)
            {
                return(true);
            }

            return(!cancellationToken.IsCancel());
        }
Example #3
0
        public async ETTask <T> Wait <T>(ETCancellationToken cancellationToken = null) where T : struct, IWaitType
        {
            ResultCallback <T> tcs = new ResultCallback <T>();
            Type type = typeof(T);

            this.tcss.Add(type, tcs);

            void CancelAction()
            {
                this.Notify(new T()
                {
                    Error = WaitTypeError.Cancel
                });
            }

            T ret;

            try
            {
                if (cancellationToken != null)
                {
                    if (!cancellationToken.IsCancel())
                    {
                        cancellationToken?.Add(CancelAction);
                    }
                    else
                    {
                        Log.Error("cancellationToken 已经取消过了");
                    }
                }
                ret = await tcs.Task;
            }
            finally
            {
                cancellationToken?.Remove(CancelAction);
            }
            return(ret);
        }