VB.NET ThreadPool Tips

Threads illustration

You want to see some examples about the basics of using the ThreadPool type in the VB.NET programming language. Using ThreadPool, you can create many threads and the ThreadPool will manage when they run for optimum performance, accounting for multiple cores and processors.

This VB tutorial shows how to use the ThreadPool type from System.Threading.

QueueUserWorkItem example

First, this example demonstrates the QueueUserWorkItem subroutine 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.

Please use the AddressOf operator and specify the method name as the first argument. You can pass anything as the second argument: it will be casted to an Object, which you can then recast in the specified method.

Program that uses ThreadPool.QueueUserWorkItem [VB.NET]

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

Program execution. This program creates a new thread in the thread pool and executes the code contained in Process() on it. It passes an argument from the main thread to the sub-thread method. Also, it relies on a hack to ensure the sub-thread completes before the enclosing program is finished; the Thread.Sleep call is not ideal.

Many threads

Steps

This ThreadPool example introduces some more concepts to our understanding of the thread pool. Two fields are used: one is an Integer that is accessed by the threaded methods; 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: the monitor calls ensure that no two threads will mutate the Integer at once and cause errors.

Program that uses loop with QueueUserWorkItem [VB.NET]

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 VB.NET programming language

Summary of results. Again, this example demonstrates the use of QueueUserWorkItem and it further shows the Monitor.Enter and Monitor.Exit methods, which are important for ThreadPool programs in VB.NET. The program correctly prints 55, which is 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.

Wait for threads

It is possible to wait for threads in the ThreadPool and not have to resort to using methods like Thread.Sleep; using methods such as WaitHandle.WaitAll is preferable. More details on WaitAll are forthcoming, but the method is demonstrated in the C# language on this site already.

ThreadPool.SetMinThreads Method

BackgroundWorker

Programming tip

Usually, developers prefer the BackgroundWorker type over the ThreadPool type in their .NET programs. The BackgroundWorker seems easier to use and more intuitive overall. For threaded programs, I almost always reach for BackgroundWorker because it is very reliable and less confusing for novices.

BackgroundWorker Use

Summary

.NET Framework information

As part of the System.Threading namespace in the .NET Framework, the ThreadPool type provides multiple threads to the VB.NET language. Usually best reserved for cases when you have many separate tasks to accomplish and not just one or two, the ThreadPool offers this important functionality and reduces the amount of thread management logic you must create and test.

VB.NET Tutorials
.NET