RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Add Multithreading to Your VB.NET Applications

Since VB.NET runs with the Common Language Runtime (CLR), it gains many new capabilities, one of which is the ability to create free-threaded applications.

evelopers have been requesting that Microsoft add more threading functionality to VB for some time—it will finally happen with VB.NET. VB6 does have support for building multithreaded EXEs, DLLs, and OCXs. But, the wording for this is somewhat misleading, in that VB6 supports running multiple single-threaded apartments. An apartment is really just a room in which code is executed and the boundaries of the apartment restrict the code from accessing anything outside of it.

VB.NET natively supports building free-threaded applications. This means that multiple threads can access the same set of shared data. The following article will walk you through the basics of multithreading.

While VB6 supports multiple single-threaded apartments, it does not support a free-threading model, which allows multiple threads to run against the same set of data. There are many situations in which spawning a new thread to run a background process would increase the usability of your application. This situation is evident when you want to place a cancel button on a form where a long process can make a form seem unresponsive.

Since VB.NET runs with the Common Language Runtime (CLR), it gains many new capabilities, one of which is the ability to create free-threaded applications.

Working with Threads
VB.NET makes it easy to start working with threads. There are some subtleties that we will explore later, but let's jump in and create a simple form that spawns a new thread to run a background process. The first thing we will need to do is create the background task that will be run on the new thread. The following code executes a rather long running process—an infinite loop:

Private Sub BackgroundProcess()
    Dim i As Integer = 1

    Do While True
        ListBox1.Items.Add("Iterations: " + i)
        i += 1
End Sub
This code loops infinitely and adds an item to a listbox on a form for each iteration. As a side note, if you are not familiar with VB.NET then you may have noticed a few other things in this code that you could not do in VB6:
  • Assign values to a variable when declaring the variable Dim i As Integer = 1
  • Use the += operator i += 1 Instead of i = i + 1
  • The Call keyword has been removed
Once we have a worker process, we need to assign this block of code to a new thread and start its execution. To do this we use the Thread object that is part of the System.Threading namespace in the .NET framework classes. When we instantiate a new Thread class we pass it a reference to the code block we want to execute in the constructor of the Thread class. The following code creates a new Thread object and passes it a reference to BackgroundProcess:
Dim t As Thread
t = New Thread(AddressOf Me.BackgroundProcess)
The AddressOf operator creates a delegate object to the BackgroundProcess method. A delegate within VB.NET is a type-safe, object-oriented function pointer. After the thread has been instantiated, you begin the execution of the code by calling the Start() method of the thread.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date