static void Main(string[] args) { FiniteObjectPool <int> pool = new FiniteObjectPool <int>(); for (int i = 0; i < 10; i++) { pool.PutObject(i); } List <Task> tasks = new List <Task>(); for (int i = 0; i < 20; i++) { int id = i; tasks.Add(Task.Run(() => { Console.WriteLine("Running task " + id); using (var con = new FiniteObjectPoolContext <int>(pool)) { Console.WriteLine("Task " + id + " got object from pool: " + con.Value); System.Threading.Thread.Sleep(5000); Console.WriteLine("Task " + id + " is finished with pool object: " + con.Value); } })); } Task.WaitAll(tasks.ToArray()); Console.WriteLine("DONE"); Console.ReadLine(); }
public void Dispose() { m_Pool.PutObject(Value); //put the object back because this context is finished }