Learn ASP.NET MVC, ASP.NET Core, and Design Patterns through our online training programs. Courses conducted by Bipin Joshi on weekends. Read more details here.

Using ThreadPool Class


You can use Thread class directly to create and run a new thread. Even though creating threads in this fashion is easy there is still easier way to do that. ThreadPool class allows you to simply place a function in a queue which is executed by the ThreadPool. ThreadPool internally maintains a pool of threads that are used to execute your code as and when required. In fact ThreadPool operations are more efficient that using Thread class directly.

Namespaces Involved

Following namespaces are involved:
  • System
  • System.Threading

QueueUserWorkItem method

The QueueUserWorkItem method of ThreadPool class simply accepts an instance of type WaitCallback. The WaitCallback class in turn accepts an address of the function you want to execute. The QueueUserWorkItem method basically puts your function in queue and executes as and when a thread is available. Following example shows how this method is used:
Public Sub QueueUserWorkItemTest()
   Dim wc As New WaitCallback(AddressOf MyWaitCallback)
End Sub

Public Sub MyWaitCallback(ByVal state As Object)
   Console.WriteLine("MyWaitCallback called")
End Sub
  • We first created an instance of WaitCallBack delegate passing it the address of our function.
  • We then call shared method - QueueUserWorkItem - of ThreadPool class that accepts this delegate as a parameter.
  • The function that will be run over the thread simply outputs some text on the console.

RegisterWaitForSingleObject method

The RegisterWaitForSingleObject method of ThreadPool class accepts an instance WaitHandle class and WaitOrTimerCallback class. WaitHandle class is typically an instance of AutoResetEvent or ManualResetEvent. These classes basically signal to the ThreadPool that your function can now be called. WaitOrTimerCallback instance is nothing but a delegate for your function. Following code shows how this method is used:
Public Sub ShowDemoRegisterWaitForSingleObject()
   Dim wtc As New 
   WaitOrTimerCallback(AddressOf MyWaitOrTimerCallback)
   Dim myevent As New AutoResetEvent(False)
   (myevent, wtc, Nothing, 20000, True)
End Sub

Public Sub MyWaitOrTimerCallback
(ByVal state As Object, ByVal timeout As Boolean)
   Console.WriteLine("MyWaitOrTimerCallback called")
End Sub
  • We first create a WaitOtTimerCallBack delegate by passing it the address of our function.
  • Next, we create an instance of AutoResetEvent.
  • RegisterWaitForSingleObject method of ThreadPool class is then called.
  • Note that unless you call Set() method on the AutoResetEvent, your function will not be executed. Calling this method indicates to ThreadPool that the method can now be called on a thread from the pool.
I hope you must have found the article useful. In the next article, I will show you how to use System.Threading.Timer class to execute your function repeatedly after certain time interval.

Bipin Joshi is a software consultant, trainer, author and a yogi having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, AngularJS, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Ajapa Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 16 Oct 2002

Tags : .NET Framework VB.NET C# Components Multithreading