Click here to Skip to main content
11,720,321 members (71,305 online)
Click here to Skip to main content

Tagged as

Thread Pooling in C# – ThreadPool.QueueUserWorkItem

, 18 Feb 2014 CPOL 17.3K 8
Rate this:
Please Sign up or sign in to vote.
A thread pool is a collection of threads that can be used to perform several tasks in the background.  This leaves the primary thread free to perform other tasks asynchronously. Once a thread in the pool completes its task, it is returned to a queue of waiting threads, where it can be reused. This r

A thread pool is a collection of threads that can be used to perform several tasks in the background.  This leaves the primary thread free to perform other tasks asynchronously.

Once a thread in the pool completes its task, it is returned to a queue of waiting threads, where it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task.

Thread pools typically have a maximum number of threads. If all the threads are busy, additional tasks are put in queue until they can be serviced as threads become available.

There are a number of ways you can enter the thread pool:

  • Via the Task Parallel Library (from Framework 4.0)
  • By calling ThreadPool.QueueUserWorkItem
  • Via asynchronous delegates
  • Via BackgroundWorker

Here I will discuss ThreadPool.QueueUserWorkItem only. In my next article I will discuss other ways.

ThreadPool.QueueUserWorkItem

One advantage of thread pooling is that you can pass arguments in a state object to the task procedure. If the procedure you are calling requires more than one argument, you can cast a structure or an instance of a class into an Object data type. It is similar to ParameterizedThreadStart.

using System;
using System.Threading;

namespace TestConsole
{
    class Program
    {
        public static void Main(string[] args)
        {
            ThreadPool.QueueUserWorkItem(DoLongTask); //Without passing input parameter
            ThreadPool.QueueUserWorkItem(DoLongTask, "Hello"); //With input parameter
            Console.WriteLine("Main thread ends");
            Console.ReadKey();
        }

        public static void DoLongTask(object input)
        {
            Console.WriteLine("Thread is background : {0}", Thread.CurrentThread.IsBackground);
            Console.WriteLine("Input parameter : {0}", input);
        }
    }
}

You should not use Sleep() or Join() methods if you are using ThreadPool. It may cause some unexpected behaviour. TheardPool’s threads are meant to complete the tasks as soon as possible without sleeping and in background.

You must explicitly deal with exceptions in the target code — un-handled exceptions will take down the program.

 Asynchronous Delegates

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Adarsh Chaurasia (Learner|Consultant|Mentor|Tech
Software Developer (Senior)
India India
I have 5.5+ years of experience in SaaS, SOA based Enterprise Web Application design and development using Microsoft technology stack. I have mostly worked on Business layer, Data access layer, WCF, Entity Framework, Microsoft Application Blocks, Search engines, APIs integration, Third party APIs/Product Research & Development.

I am a huge fan of Design Patterns. I also work as Software Consultant. I read/write blogs, help and learn from other developers.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Akhil Mittal 23-Nov-14 19:42
mvp Akhil Mittal 23-Nov-14 19:42 
SuggestionExplain in detail Pin
Rhutesh K21-Nov-14 3:40
memberRhutesh K21-Nov-14 3:40 
GeneralRe: Explain in detail Pin
Adarsh Chaurasia (Consultant|Mentor|Tech Savvy)25-Nov-14 4:26
memberAdarsh Chaurasia (Consultant|Mentor|Tech Savvy)25-Nov-14 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150901.1 | Last Updated 18 Feb 2014
Article Copyright 2014 by Adarsh Chaurasia (Learner|Consultant|Mentor|Tech
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid