| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 | using System;using System.Collections.Generic;using System.Linq;namespace Fort23.Core{    public class CTaskAwaitBuffer<T> : IDisposable where T : new()    {        private List<CTask<T>> _cTasks = new List<CTask<T>>();                private List<T> _results = new List<T>();        public CTask<T>[] GetTask        {            get { return _cTasks.ToArray(); }        }        private int count;        private CTask CTask;        private bool _isWait;                              public T[] GetResults()        {                        return _results.ToArray();                    }        public void Clear()        {            _cTasks.Clear();            _isWait = false;            CTask = null;            count = 0;        }        public void AddTask(CTask<T> icTaskResult)        {            _cTasks.Add(icTaskResult);            if (_isWait)            {                count++;                AwaitTask(icTaskResult);            }        }        public async CTask<List<T>> WaitAll()        {            _isWait = true;            count += _cTasks.Count;            if (count == 0)            {                return new List<T>();            }            CTask = CTask.Create(false);            CTask<T>[] icTaskResults = _cTasks.ToArray();            for (int i = 0; i < icTaskResults.Length; i++)            {                AwaitTask(icTaskResults[i]);            }            await CTask;            return new List<T>(_results);        }        public async CTask AwaitTask(CTask<T> icTaskResult)        {            T result = await icTaskResult;            _results.Add(result);            count--;            // _cTasks.Remove(icTaskResult);            if (count <= 0 && _isWait)            {                _isWait = false;                CTask cta = CTask;                // if (_cTasks != null)                // {                //     _cTasks.Clear();                // }                count = 0;                cta.SetResult();            }        }        public void Dispose()        {            _cTasks.Clear();            _results.Clear();            CTask = null;            _isWait = false;        }    }}
 |