Multi-Threading using Thread Pool C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssouhrada
    New Member
    • Jun 2008
    • 12

    Multi-Threading using Thread Pool C#

    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();
                    }
                }
            }
        }
    }
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    Hi

    If I understand this correctly, you want to be able to read the output before the application exits? If so:

    Code:
    Console.ReadLine();
    Will fix that problem. This will cause the application to be idle until the <ENTER> key is pressed.

    Comment

    • ssouhrada
      New Member
      • Jun 2008
      • 12

      #3
      Possible Solution

      I have tried doing that but it will get the Console.Readlin e(); prior to actually executing all of the statements. The point is also to not have to require user interaction as I'm building an app that would hopefully not require supervision and could be scheduled to run. I did end up finding an answer on another post. Here is the code that ended up working as desired:


      Code:
      using System;
      using System.Collections.Generic;
      using System.Threading;
      namespace ThreadPoolWaitForAllThread
      {
          class Program
          {
              static void Main(string[] args)
              {
                  List<ManualResetEvent> events = new List<ManualResetEvent>();
                  ThreadPool.SetMinThreads(500, 500);
                  for (int i = 0; i < 100000; i++)
                  {
                      ThreadPoolObj obj = new ThreadPoolObj();
                      obj.ObjectID = i;                
                      obj.signal = new ManualResetEvent(false);
                      events.Add(obj.signal);
                      WaitCallback callback = new WaitCallback(ThreadFunction);
                      ThreadPool.QueueUserWorkItem(callback, obj);
                  }
                  WaitForAll(events.ToArray());
                  Console.WriteLine("Completed");
                  Console.ReadLine();
      
      
                  
      
              }
              static bool WaitForAll(ManualResetEvent[] events)
              {
                  bool result = false;
                  try
                  {
                      if (events != null)
                      {
                          for (int i = 0; i < events.Length; i++)
                          {
                              events[i].WaitOne();
                          }
                          result = true;
                      }
                  }
                  catch
                  {
                      result = false;
                  }
                  return result;
              }
              static void ThreadFunction(object threadobj)
              {
                  ThreadPoolObj obj = threadobj as ThreadPoolObj;
                  if (obj != null)
                  {
                      Console.WriteLine(obj.ObjectID.ToString());
                      Thread.Sleep(2000); // Just Wait To Show Syncronization 
                      obj.signal.Set();
                  }
              }
          }
          class ThreadPoolObj
          {
              public int ObjectID;
              public ManualResetEvent signal;
          }
      }

      Comment

      Working...