How do I use ThreadPool in C# and .NET?

In this article, I will briefly talk about thread pool and how to utilize thread pool from C# applications.

How to Assign Thread in ThreadPool ?

ThreadPool.QueueUserWorkItem(new WaitCallback(FunctionCall), PersonalizeDataForTherad);

How To Assign Timer Thread in ThreadPool ?

ThreadPool.RegisterWaitForSingleObject(
AutoResetEventObject,
new WaitOrTimerCallback(FunctionCall),
PersonalizeData,
TimeOut,
false
);

Some Points :

  • There is no way to cancel a work item after it has been queued.
  • Each process can have only one operating system thread pool. 

The thread pool is created the first time you create an instance of the ThreadPool class. The thread pool has a default limit of 25 threads per available processor, which could be changed using CorSetMaxThreads as defined in the mscoree.h file. Each thread uses the default stack size and runs at the default priority. Each process can have only one operating system thread pool.

There is only one ThreadPool object per process. The thread pool is created the first time you call ThreadPool.QueueUserWorkItem, or when a timer or registered wait operation queues a callback method. One thread monitors all tasks that have been queued to the thread pool. When a task has completed, a thread from the thread pool executes the corresponding callback method. There is no way to cancel a work item after it has been queued.

The number of operations that can be queued to the thread pool is limited only by available memory; however, the thread pool will enforce a limit on the number of threads it allows to be active in the process simultaneously (which is subject to the number of CPUs and other considerations). Each thread uses the default stack size, runs at the default priority, and is in the multithreaded apartment. If one of the threads becomes idle (as when waiting on an event) in managed code, the thread pool injects another worker thread to keep all the processors busy. If all thread pool threads are constantly busy, but there is pending work in the queue, the thread pool will, after some period of time, create another worker thread. However, the number of threads will never exceed the maximum value. The ThreadPool also switches to the correct AppDomain when executing ThreadPool callbacks. 

There are several scenarios in which it is appropriate to create and manage your own threads instead of using the ThreadPool. You should do so:  

  • If you require a task to have a particular priority.
  • If you have a task that might run a long time (and therefore block other tasks).
  • If you need to place threads into a single-threaded apartment (all ThreadPool threads are in the multithreaded apartment).
  • If you need to have a stable identity associated with the thread. For example, you might want to use a dedicated thread to abort that thread, suspend it, or discover it by name.

More About ThreadPool :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingthreadpoolclassqueueuserworkitemtopic.asp


Similar Articles