HOW TO USE THREAD POOL FOR SOME REPETITIVE TASK IN C# ?

Total Post:135

Points:949
C#
 1061  View(s)
Ratings:
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

    Points:949
    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
                StartProcess();
                Console.WriteLine("thread {0}  is now ended...", threadIndex);
              // check that process has been completed
                this.doneEvent.Set();
            }
           // start any process
            public void StartProcess()
            {
                Process.Start("Notepad.exe");
            }
        }
        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
                    Thread.Sleep(2000);
                }
               // Wait for all thread for completion
                WaitHandle.WaitAll(sendEvents);
                Console.WriteLine("All Notepad window are opened .");
                Console.ReadKey();
            }
        }
    }

     

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!