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#.?
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 { classMyProcess { // for doing this create an event publicManualResetEvent doneEvent; // Constructors public MyProcess(ManualResetEvent sendEvent) { // Assign notify event from Thread Pool this.doneEvent = sendEvent; } // Initialising publicvoid 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 publicvoid StartProcess() { Process.Start("Notepad.exe"); } } publicclassThreadPoolEx { staticvoid Main() { constint total = 10; // Create ManualResetEvent array to assign with process ManualResetEvent[] sendEvents = newManualResetEvent[total]; MyProcess[] MyProcessArray = newMyProcess[total]; Console.WriteLine("Starting thread..."); for (int i = 0; i < total; i++) { sendEvents[i] = newManualResetEvent(false); MyProcess p = newMyProcess(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(); } } }
How to use thread pool for some repetitive task in c# ?
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Join MindStick Community
You have need login or register for voting of answers or question.
Anupam Mishra
08-Jan-2016In 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: