Introduction to Thread Pool and Invoking a delegare on Thread Pool

What is Thread Pool and how to invoke a delegate on Thread Pool

Category: Multithreading Tags: C#


        What is Thread Pool? Thread pool is an organizer of threads which provides a thread when needed, once operation is completed thread is returned to pool. Whenever we put a new operation on thread pool it is possible that it reuses same thread which completed previous operation instead of creating a new thread. So it will reduce overhead of creating new thread every time, we should use thread pool when threads are short lived.
Suppose an application creates many threads and completes many at same time. Creating many threads every second will be extra overhead instead we can use same threads which Are completed their operations. Below are some benefits of using thread pool:

  1. Reduces extra overhead and complexity
  2. Easy to manage multiple threads
  3. Increase in performance
  4. Supports callback methods

This article we are going to learn how invoke a delegate on pool and callback which leads us towards APM (Asynchronous Programming Model).


    Let’s create a method which we will execute on thread, and a delegate of same signature:

delegate int ThreadPoolFun();
public static int Demo()
    Console.WriteLine("Worker Started");
    Console.WriteLine("Is this thread on thread pool: " + Thread.CurrentThread.IsThreadPoolThread);
    Console.WriteLine("Thread pool worker thread id: " + Thread.CurrentThread.ManagedThreadId);
    return Thread.CurrentThread.ManagedThreadId;

Above we created a delegate ThreadPoolFun and a method Demo with same signature, Demo is printing whether thread is on pool or not, thread id and returns thread id. Now we will create a callback method which accepts parameter IAsyncResult which will be called once thread completes execution:

public static void CallBack(IAsyncResult result)
    Console.WriteLine("Callback Started");
    Console.WriteLine("State passed to callback: " + result.AsyncState);
    Console.WriteLine("Is on thread pool: " + Thread.CurrentThread.IsThreadPoolThread);
    Console.WriteLine("Thread pool worker thread id: " + Thread.CurrentThread.ManagedThreadId);

Now in Main method we will execute Demo on thread and register the CallBack method:

static void Main(string[] args)
    ThreadPoolFun fun = new ThreadPoolFun(Demo);
    IAsyncResult asyncResult = fun.BeginInvoke(CallBack, "Delegate asynchronous call");
    int result = fun.EndInvoke(asyncResult);
    Console.WriteLine("Thread id of Demo is: " + result);

Above we created object of delegate and executing thread using BeginInvoke method where we registered CallBack method which will be called once thread execution is completed. AsyncWaitHandle waits until operation is completed, as soon as thread is completed CallBack will be called. AsyncWaitHandle is not required to call because EndEnvoke will call it internally. Output of above code will be:

Worker Started
Is this thread on thread pool: True
Thread pool worker thread id: 10
Thread id of Demo is: 10
Callback Started
State passed to callback: Delegate asynchronous call
Is on thread pool: True
Thread pool worker thread id: 10

Like 0 People
Last modified on 15 June 2018
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 165
Questions: 16
Given Best Solutions: 16 *


No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User