Click here to Skip to main content
Click here to Skip to main content

Tagged as

Thread Pooling in C# – ThreadPool.QueueUserWorkItem

, 18 Feb 2014
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 (Consultant|Mentor|Tech Savvy)
Software Developer (Senior)
India India
I have 4.6+ 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.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 18 Feb 2014
Article Copyright 2014 by Adarsh Chaurasia (Consultant|Mentor|Tech Savvy)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid