VB.NET ThreadPool

Use the ThreadPool type from System.Threading. Pass a WaitCallback argument.

ThreadPool handles many threads. It allows us to easily manage these threads. We optimize when they run for the best performance. We account for multiple cores and processors. ThreadPool eliminates our need to implement this logic.

Example. First, this example uses the QueueUserWorkItem sub on the ThreadPool type. The QueueUserWorkItem method receives two parameters: a WaitCallback instance, which is assigned to a subroutine address, and an argument of type Object.

AddressOf: Please use the AddressOf operator and specify the method name as the first argument.

Tip: You can pass anything as the second argument. It will be cast to an Object, which you can then recast in the specified method.

Tip 2: Pay attention to how the WaitCallback is passed as an argument. This syntax is a bit confusing.

Info: This program creates a new thread in the thread pool and executes the code. It passes an argument from the main thread to the sub-thread method.

VB.NET program that uses ThreadPool.QueueUserWorkItem Imports System.Threading Module Module1 Sub Main() ' Use this argument to the thread. Dim t As String = "test" ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Process), t) ' Wait a while. Thread.Sleep(50) End Sub Sub Process(ByVal arg As Object) ' Cast the argument and display its length. Dim t As String = arg Console.WriteLine(t.Length) End Sub End Module Output 4

Example 2. This ThreadPool example introduces some more concepts to our understanding of the thread pool. Two fields are used. One is an Integer (_int) that is accessed by the threaded methods.

And: The second is an Object that is used by the Monitor.Enter and Monitor.Exit subroutines.


First: A loop creates ten threaded calls to the subroutine Process. In Process we recast the argument and then invoke Monitor.Enter and Monitor.Exit.

Tip: The monitor calls ensure that no two threads will mutate the Integer at once.

Important: This example demonstrates the use of QueueUserWorkItem and it further shows the Monitor.Enter and Monitor.Exit methods.

VB.NET program that uses loop with QueueUserWorkItem Imports System.Threading Module Module1 Private _int As Integer Private _obj As Object = New Object Sub Main() ' WaitCallback instance. Dim w As WaitCallback = New WaitCallback(AddressOf Process) ' Loop over these values. For var As Integer = 0 To 10 ThreadPool.QueueUserWorkItem(w, var) Next ' Wait a while. Thread.Sleep(50) ' Write integer. Console.WriteLine(_int) End Sub Sub Process(ByVal arg As Object) ' Cast the argument and display it. Dim t As Integer = arg ' Lock the integer. Monitor.Enter(_obj) ' Change value. _int += t ' Unlock. Monitor.Exit(_obj) End Sub End Module Output 55

The program prints 55, the sum of all numbers in the range of 0 to 10. It did this by using 10 threaded method calls. On the ThreadPool, these could be run on any number of processors, determined by the framework and your hardware.

BackgroundWorker. ThreadPool is not your only option. Usually, developers prefer the BackgroundWorker type over the ThreadPool type in their .NET programs. The BackgroundWorker seems easier to use and overall more intuitive.

Note: For threaded programs, I almost always reach for BackgroundWorker because it is very reliable and less confusing for novices.


Summary. As part of the System.Threading namespace, the ThreadPool type provides multiple threads support in the VB.NET language. It is usually best reserved for cases when you have many separate tasks to accomplish, not just one or two.

Review: ThreadPool reduces the amount of thread management logic you must create and test.

Dot Net Perls