---
title: "How to use thread pool  for some repetitive task in c# ?"  
description: "How to use thread pool  for some repetitive task in c# ?"  
author: "Anupam Mishra"  
published: 2016-01-07  
updated: 2016-01-08  
canonical: https://www.mindstick.com/forum/33844/how-to-use-thread-pool-for-some-repetitive-task-in-c-sharp  
category: "c#"  
tags: ["c#", "thread", "threading"]  
reading_time: 2 minutes  

---

# How to use thread pool  for some repetitive task in c# ?

I want to write Thread [Pool](https://yourviews.mindstick.com/view/298/a-pool-of-schemes-yet-not-benefiting-all) using C# for some repetitive [task](https://www.mindstick.com/forum/161761/what-is-the-difference-between-task-and-thread) which is [controlled](https://yourviews.mindstick.com/view/81837/has-pakistan-controlled-corona-pandemic) through thread Scheduler.thread Scheduler will run after interval of 2 minutes.Can you please provide me few [guidelines](https://www.mindstick.com/blog/245/software-requirements-and-guidelines-for-developing-android-application) regarding how to write thread pool in C#.?

## Replies

### Reply by Anupam Mishra

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();
> }
> }}
> ```

![How to use thread pool  for some repetitive task in c# ?](https://www.mindstick.com/mindstickforums/535ae8b9-2e92-49eb-a041-e901e78d199f/images/f4618af0-d42c-4eab-8c0b-08d0eb68249a.png)\


---

Original Source: https://www.mindstick.com/forum/33844/how-to-use-thread-pool-for-some-repetitive-task-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
