/// <summary>
 /// Initialize our worker threads.
 /// </summary>
 protected void StartThreads()
 {
     for (int i = 0; i < MAX_WORKER_THREADS; i++)
     {
         Thread thread = new Thread(new ParameterizedThreadStart(ProcessRequestThread));
         thread.IsBackground = true;
         ThreadSemaphore ts = new ThreadSemaphore();
         threadPool.Add(ts);
         thread.Start(ts);
     }
 }
        /// <summary>
        /// A thread method that waits for work on its queue and then processes that work via
        /// the context's workflow engine.
        /// </summary>
        protected void ProcessRequestThread(object state)
        {
            ThreadSemaphore ts = (ThreadSemaphore)state;

            while (true)
            {
                ts.WaitOne();
                WorkflowContext context;

                if (ts.TryDequeue(out context))
                {
                    // Continue with where we left off for this context's workflow.
                    context.WorkflowContinuation.Workflow.Continue(context.WorkflowContinuation, context.Context);
                }
            }
        }
        /// <summary>
        /// A thread method that waits for work on its queue and then processes that work via
        /// the context's workflow engine.
        /// </summary>
        protected void ProcessRequestThread(object state)
        {
            ThreadSemaphore ts = (ThreadSemaphore)state;

            while (true)
            {
                ts.WaitOne();
                WorkflowContext context;

                if (ts.TryDequeue(out context))
                {
                    // Wait until we exit the workflow internal continue from a defering state.
                    while (!context.WorkflowContinuation.Deferred)
                    {
                        Thread.Sleep(0);
                    }
                    // Continue with where we left off for this context's workflow.
                    context.WorkflowContinuation.Workflow.Continue(context.WorkflowContinuation, context.Context);
                }
            }
        }