ThreadPool.QueueUserWorkItem Multi-threading Code Sample

By Robbe Morris

Here is a barebone example of how to use the ThreadPool to queue up tasks in a multi-threaded environment.

using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace MyTest
{
class Tester
{

        private static List<ManualResetEvent> _waitHandles = null;

        static void Main(string[] args)
  {
             
             int defaultMaxworkerThreads = 0;
             int defaultmaxIOThreads = 0;
             int waitHandleIndex = 0;
             int maxThreadsAtOnce = 35;

             try
            {

                _waitHandles = new List<ManualResetEvent>();
                waitHandleIndex = 0;

               ThreadPool.GetMaxThreads(out defaultMaxworkerThreads, out defaultmaxIOThreads);
               ThreadPool.SetMaxThreads(defaultMaxworkerThreads, defaultmaxIOThreads);

                 int start = 0;
  
                 for (int i = start; i < 5000000; i++)
                {

                   Console.WriteLine(i.ToString());

                     var resetEvent = new ManualResetEvent(false);
                     var controller = new MyTestJob(waitHandleIndex);

                   ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(OnThreadedDataRequest), controller);

                      waitHandleIndex++;
                     _waitHandles.Add(resetEvent);

                     if (_waitHandles.Count >= maxThreadsAtOnce)
                    {
                       System.Threading.WaitHandle.WaitAll(_waitHandles.ToArray<ManualResetEvent>());
                        _waitHandles = new List<ManualResetEvent>();
                        waitHandleIndex = 0;
                    }

                 }

                 if (_waitHandles.Count > 0) System.Threading.WaitHandle.WaitAll(_waitHandles.ToArray<ManualResetEvent>());

             }
             catch (Exception ex)
            {
               Debug.WriteLine(ex.Message + " " + ex.StackTrace);
            }

           Console.WriteLine("done");
           Console.ReadLine();
        }
    
        public static void OnThreadedDataRequest(object sender)
        {
             var controller = sender as MyTestJob;
             if (controller == null) return;
            controller.Execute();
            _waitHandles[controller.WaitHandleIndex].Set();
        }

    }

    public class MyTestJob
    {

         public int WaitHandleIndex = 0;

        public MyTestJob(int waitHandleIndex)
        {
            WaitHandleIndex = waitHandleIndex;
             // Add input parameters to the constructor to store input variables in class level variables
            // for use by the Execute method below.
        }

         public void Execute()
        {

             try
            {
               Console.WriteLine(WaitHandleIndex.ToString() + " - " + DateTime.Now.ToString());
             }
             catch (Exception ex)
            {
               Debug.WriteLine(ex.Message + " " + ex.StackTrace);
            }

        }
    }

}

ThreadPool.QueueUserWorkItem Multi-threading Code Sample  (5742 Views)