/// <summary> /// Either ends the thread by returning, or waits for another call to RunThreadStart /// </summary> private void RunPooledThread() { Wrapped<ThreadStart> toBePulsed = new Wrapped<ThreadStart>(); while (true) { // Kill the thread if there is already enough idle threads if (CurrentIdleThreads.Count >= NumIdleThreads) return; CurrentIdleThreads.Push(toBePulsed); lock(toBePulsed) Monitor.Wait(toBePulsed); toBePulsed.Value(); } }