Click here to Skip to main content
12,892,810 members (54,974 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 6 Nov 2011

Controlled ThreadPool using .NET Framework 4.0

, 8 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to do controlled ThreadPooling using .NET Framework 4.0.


Controlled ThreadPool using BlockingCollection in .NET Framework 4.0.


A very common requirement is to create a pool of defined number of threads and execute tasks on it. For years, We have been controlling the tasks queue collection using locks and having all threads running all the time even when there are no tasks in queue. Another way is to use ManualResetEvents as explained here nicely:

Recently I came across the new Threading class (BlockingCollection) available in .NET Framework 4.0 and found that now we can do the same job without locks and without running threads all the time. Also it require less line of neat code.

Using the code

This class is written using generics which means it can store any type of task and one handler for those tasks. It can also be used as a collection for multiple handlers.

public class OrderlyThreadPool<t> : IDisposable
    BlockingCollection<t> _workingqueue = null;
    Action<t> _handler = null;
    public OrderlyThreadPool(int wrkerCount, Action<t> handler)
        _workingqueue = new BlockingCollection<t>();
        _handler = handler;
        Worker worker = new Worker(wrkerCount, Process);
    public void AddItem(T item)
    private void Process()
        foreach (T item in _workingqueue.GetConsumingEnumerable())
    public void Dispose() { _workingqueue.CompleteAdding(); 
                            _workingqueue = null; }
 * Class which handles the workers on the queue 
public class Worker
    int _wrkerCount = 0;
    Action _action = null;
    public Worker(int workerCount, Action action)
        _wrkerCount = workerCount;
        _action = action;
    public void Start()
        // Create and start a separate Task for each consumer:
        for (int i = 0; i < _wrkerCount; i++)


static void main()
   OrderlyThreadPool<city> threadpool = new OrderlyThreadPool<city>(6, new Action<city>(Handler));
                threadpool.AddItem(new City() { CityZipCode = "60661",CityName ="Chicago",State="Illinois" });
static void Handler(City c)
class City
    public string CityName;
    public string CityZipCode;
    public string State;

Note: This class requires reference to System.Collections.Concurrent,System.Threading.Tasks and System.Collections.Generic

Points of Interest

Threading has been improved significantly with many new classes like LocalThread.


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


About the Author

Software Developer (Senior) Bank of America Merrill Lynch
United States United States
Software developer with 7 years of experience in MS technologies. Also love to write code using open source like python,java.

You may also be interested in...


Comments and Discussions

GeneralCity???- what is wrong??? Pin
j_alles8-Nov-11 9:19
memberj_alles8-Nov-11 9:19 
GeneralRe: j_alles, Thanks for bringing in notice the Caller code. I h... Pin
manish313838-Nov-11 13:49
membermanish313838-Nov-11 13:49 
Generalfor some reason <City> does not print out-----????? Pin
j_alles8-Nov-11 9:18
memberj_alles8-Nov-11 9:18 
GeneralThe <City> was not printing out: It should be--> OrderlyThr... Pin
j_alles8-Nov-11 9:18
memberj_alles8-Nov-11 9:18 
GeneralSorry, This should be the line in Main(): OrderlyThreadPo... Pin
j_alles8-Nov-11 9:16
memberj_alles8-Nov-11 9:16 
General---> I had to make the following changes to get your code wo... Pin
j_alles8-Nov-11 9:12
memberj_alles8-Nov-11 9:12 
QuestionPass additional data to each thread Pin
Omar Al Zabir8-Nov-11 3:35
mvpOmar Al Zabir8-Nov-11 3:35 
AnswerRe: Pass additional data to each thread Pin
manish313838-Nov-11 13:31
membermanish313838-Nov-11 13:31 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 8 Nov 2011
Article Copyright 2011 by manish31383
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid