123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System;
- using System.Collections.Generic;
- namespace ET
- {
- public static class ETTaskHelper
- {
- public static bool IsCancel(this ETCancellationToken self)
- {
- if (self == null)
- {
- return false;
- }
- return self.IsDispose();
- }
-
- private class CoroutineBlocker
- {
- private int count;
- private ETTask tcs;
- public CoroutineBlocker(int count)
- {
- this.count = count;
- }
-
- public async ETTask RunSubCoroutineAsync(ETTask task)
- {
- try
- {
- await task;
- }
- finally
- {
- --this.count;
-
- if (this.count <= 0 && this.tcs != null)
- {
- ETTask t = this.tcs;
- this.tcs = null;
- t.SetResult();
- }
- }
- }
- public async ETTask WaitAsync()
- {
- if (this.count <= 0)
- {
- return;
- }
- this.tcs = ETTask.Create(true);
- await tcs;
- }
- }
- public static async ETTask WaitAny(List<ETTask> tasks)
- {
- if (tasks.Count == 0)
- {
- return;
- }
- CoroutineBlocker coroutineBlocker = new CoroutineBlocker(1);
- foreach (ETTask task in tasks)
- {
- coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
- }
- await coroutineBlocker.WaitAsync();
- }
- public static async ETTask WaitAny(ETTask[] tasks)
- {
- if (tasks.Length == 0)
- {
- return;
- }
- CoroutineBlocker coroutineBlocker = new CoroutineBlocker(1);
- foreach (ETTask task in tasks)
- {
- coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
- }
- await coroutineBlocker.WaitAsync();
- }
- public static async ETTask WaitAll(ETTask[] tasks)
- {
- if (tasks.Length == 0)
- {
- return;
- }
- CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Length);
- foreach (ETTask task in tasks)
- {
- coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
- }
- await coroutineBlocker.WaitAsync();
- }
- public static async ETTask WaitAll(List<ETTask> tasks)
- {
- if (tasks.Count == 0)
- {
- return;
- }
- CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Count);
- foreach (ETTask task in tasks)
- {
- coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
- }
- await coroutineBlocker.WaitAsync();
- }
- }
- }
|