How to use thread pool for some repetitive task in c# ?

Total Post:135

 1219  View(s)
Rate this:
I want to write Thread Pool using C# for some repetitive task which is controlled through thread Scheduler.thread Scheduler will run after interval of 2 minutes.Can you please provide me few guidelines regarding how to write thread pool in C#.?
  1. Post:135

    Re: How to use thread pool for some repetitive task in c# ?

    In this scenario, we executes 10 times to open a Notepad using Thread Pool. Thread scheduler will run after interval of 2 minutes to open is new one.

    So, we using thread pool such like this:
    using System.Threading;
    using System.Diagnostics;
    using System;
    namespace PoolThread
        class MyProcess
            // for doing this create an event
           public ManualResetEvent doneEvent;
            // Constructors
            public MyProcess(ManualResetEvent sendEvent)
             // Assign notify event from Thread Pool
                this.doneEvent = sendEvent;
           // Initialising
            public void MyProcessThreadPoolCallback(Object index)
                int threadIndex = (int)index;
                Console.WriteLine("thread {0} is now started...", threadIndex);
                // Call the process for every thread
                Console.WriteLine("thread {0}  is now ended...", threadIndex);
              // check that process has been completed
           // start any process
            public void StartProcess()
        public class ThreadPoolEx
            static void Main()
                const int total = 10;
                // Create ManualResetEvent array to assign with process
                ManualResetEvent[] sendEvents = new ManualResetEvent[total];
                MyProcess[] MyProcessArray = new MyProcess[total];
                Console.WriteLine("Starting thread...");
                for (int i = 0; i < total; i++)
                    sendEvents[i] = new ManualResetEvent(false);
                    MyProcess p = new MyProcess(sendEvents[i]);
                    MyProcessArray[i] = p;
                      // QueueUserWorkItem () method to execute when thread pool having thread
                    ThreadPool.QueueUserWorkItem(p.MyProcessThreadPoolCallback, i);
                    //It goes to sleep mode for 2 second
               // Wait for all thread for completion
                Console.WriteLine("All Notepad window are opened .");


      Modified On Apr-10-2018 10:56:26 PM