using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Esiur.Engine { public class AsyncBag:AsyncReply { Dictionary results = new Dictionary(); int count = 0; bool sealedBag = false; public void Then(Action callback) { base.Then(new Action(o => callback((T[])o))); } public void Seal() { sealedBag = true; if (results.Count == 0) Trigger(new T[0]); for(var i = 0; i < results.Count; i++) //foreach(var reply in results.Keys) results.Keys.ElementAt(i).Then((r) => { results[results.Keys.ElementAt(i)] = (T)r; count++; if (count == results.Count) Trigger(results.Values.ToArray()); }); } public void Add(AsyncReply reply) { if (!sealedBag) results.Add(reply, default(T)); } public AsyncBag() { } } }