I have some code below which Queues up Worker Items... The only thing it doesn't do is wait for the application to finish executing it. Any ideas on how I can get the application wait for all these Console.Writeli nes to actually display on the screen? It exits out before it's finished.
Code:
using System; using System.Threading; namespace ThreadPoolTest { class Program { private static int numBusy = 10000; private const int NumThreads = 10000; private static ManualResetEvent doneEvent; private static void Main(string[] args) { ThreadPool.SetMinThreads(100, 100); doneEvent = new ManualResetEvent(false); for (int s = 0; s < NumThreads; s++) { ThreadPool.QueueUserWorkItem( new WaitCallback(DoWork), (object)s); } doneEvent.WaitOne(); Console.WriteLine("DONE"); } private static void DoWork(object o) { try { // do work here int index = (int)o; Console.WriteLine("Index: {0} Numbusy {1}: ", index, numBusy); //Thread.Sleep(100); //Console.WriteLine("DONE: {0} Numbusy {1}: ", index, numBusy); } catch { // error handling goes here } finally { if (Interlocked.Decrement(ref numBusy) == 0) { doneEvent.Set(); } } } } }
Comment