Home > DeveloperSection > Forums > How to use thread pool for some repetitive task in c# ?
Anupam Mishra

Total Post:135

Posted on    January-07-2016 10:19 PM

 C# C#  Thread  Threading 

 1 Reply(s)
 395  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#.?

Anupam Mishra

Total Post:135

Posted on    January-08-2016 1:57 AM

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 Jan-13-2016 10:00:41 PM

Don't want to miss updates? Please click the below button!

Follow MindStick