---
title: "ThreadPool class in C#"  
description: "In this blog I will told you that how to use ThreadPool class for creating thread in c#. Apart from this I will also told you that when to use ThreadP"  
author: "Anonymous User"  
published: 2012-04-15  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/296/threadpool-class-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# ThreadPool class in C#

[ThreadPool](https://www.mindstick.com/articles/622/threadpool-in-c-sharp-programming) [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) is used to manage a set of readily available [threads](https://www.mindstick.com/blog/11678/threads-in-java) to be used by a process. We can use ThreadPool class for creating threads when we require [large](https://www.mindstick.com/forum/161063/how-to-optimize-entity-framework-core-queries-for-large-datasets) number of short living threads. A short living thread is a thread that is created to perform a single task and then terminated.We can use ThreadPool class when it is not necessary to have full [control](https://www.mindstick.com/blog/197/asp-dot-net-repeater-control) over each individual threads.\

##### Note: There is a limit of 25 threads in a thread pool. That means if you

##### start 50 threads then only 25 threads is active at a time. As soon as a

##### thread finishes another thread is active from thread pool.

##### Avoiding uses of ThreadPool class when following situation raise

##### 1. Requires a foreground thread.

##### 2. Requires a thread to have a particular priority.

3. Have task that can [block](https://www.mindstick.com/forum/157599/explain-the-process-control-block-pcb-in-the-operating-system) threads for long [period](https://yourviews.mindstick.com/story/2247/some-great-indian-dynasties-before-mughal-period) of time. The threadpool has a maximum number of threads, so a large number of threadsfrom the [thread pool](https://www.mindstick.com/forum/33844/how-to-use-thread-pool-for-some-repetitive-task-in-c-sharp) being blocked might prevent task from starting.

##### 4. Need to have stable identity associated with thread.

##### 5. Need to place threads into a single threaded apartment.

##### Implementation of ThreadPool class

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace ThreadStartDemo{    class ThreadPoolClassDemo    {        static void Main(string[] args)        {            //Call QueueUserWorkItem method of ThreadPool claas            //to send Thread in ThreadPool and pass object of            //WaitCallback deleaget.            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));            Console.WriteLine("Main Thread Started.");            Thread.Sleep(10000);            Console.WriteLine("Main Thread Exists.");        }        /// <summary>        /// This method is passed to WaitCallback delegate.        /// This method is used to print numbers from 1 to 50.        /// Main thread exists in two case either time fiven in main sleep        /// method over of number is printed from 1 to 50.        /// </summary>        /// <param name="stateInfo"></param>        static void ThreadProc(object stateInfo)        {            Console.WriteLine("Welcome from thread pool. I will count from 1 to 50");            for (int i = 1; i <= 50; i++)            {                Console.WriteLine(i);                Thread.Sleep(500);            }        }    }}
```

[Execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) above [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to see the output.

---

Original Source: https://www.mindstick.com/blog/296/threadpool-class-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
