private void StartThreads() { for (int i = 0; i < MAX_WORKER_THREADS; i++) { Thread thread = new Thread(new ParameterizedThreadStart(ProcessThread)) { IsBackground = true }; ThreadSemaphore ts = new ThreadSemaphore(); threadPool.Add(ts); thread.Start(ts); } }
/// <summary> /// As a thread, we wait until there's something to do /// </summary> /// <param name="state"></param> private void ProcessThread(object state) { ThreadSemaphore ts = (ThreadSemaphore)state; while (true) { ts.WaitOne(); HttpListenerContext context; if (ts.TryDequeue(out context)) { Program.TimeStamp("Processing on thread " + Thread.CurrentThread.ManagedThreadId); _handler.CommonResponse(context); } } }