Add Multithreading to Your VB.NET Applications

Add Multithreading to Your VB.NET 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    LoopEnd 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 variableDim i As Integer = 1
  • Use the += operatori += 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 Threadt = New Thread(AddressOf Me.BackgroundProcess)t.Start()

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.

Keep It Under Control
After the thread is started, you have some control over the state of it by using methods of the Thread object. You can pause a thread’s execution by calling the Thread.Sleep method. This method takes an integer value that determines how long the thread should sleep. If you wanted to slow down the addition of items to the listbox in the example above, place a call to the sleep method in this code:

Private Sub BackgroundProcess()    Dim i As Integer = 1    Do While True        ListBox1.Items.Add("Iterations: " + i)        i += 1        Thread.CurrentThread.Sleep(2000)    LoopEnd Sub

CurrentThread is a public static property that allows you to retrieve a reference to the currently running thread.

You can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep (System.Threading.Timeout.Infinite). To interrupt this sleep you can call the Thread.Interrupt method.

Similar to Sleep and Interrupt are Suspend and Resume. Suspend allows you to block a thread until another thread calls Thread.Resume. The difference between Sleep and Suspend is that the latter does not immediately place a thread in the wait state. The thread does not suspend until the .NET runtime determines that it is in a safe place to suspend it. Sleep will immediately place a thread in a wait state.

Lastly, Thread.Abort stops a thread from executing. In our simple example, we would want to add another button on the form that allows us to stop the process. To do this all we would have to do is call the Thread.Abort method as follows:

Private Sub Button2_Click(ByVal sender As System.Object, _        ByVal e As System.EventArgs) Handles Button2.Click    t.Abort()End Sub

This is where the power of multithreading can be seen. The UI seems responsive to the user because it is running in one thread and the background process is running in another thread. The cancel button immediately responds to the user’s click event and processing stops.



Passing Data Through Multithreaded Procedures
The last example shows a rather simple situation. Multithreading has many complications that you have to work out when you program. One issue that you will run into is passing data to and from the procedure passed to the constructor of the Thread class. That is to say, the procedure you want to kick off on another thread cannot be passed any parameters and you cannot return data from that procedure. This is because the procedure you pass to the thread constructor cannot have any parameters or return value. To get around this, wrap your procedure in a class where the parameters to the method are written as fields of the class.

A simple example of this would be if we had a procedure that calculated the square of a number:

Function Square(ByVal Value As Double) As Double    Return Value * ValueEnd Function

To make this procedure available to be used in a new thread we would wrap it in a class:

Public Class SquareClass    Public Value As Double    Public Square As Double    Public Sub CalcSquare()        Square = Value * Value    End SubEnd Class

Use this code to start the CalcSquare procedure on a new thread. following code:

Private Sub Button1_Click(ByVal sender As System.Object, _        ByVal e As System.EventArgs) Handles Button1.Click    Dim oSquare As New SquareClass()    t = New Thread(AddressOf oSquare.CalcSquare)    oSquare.Value = 30    t.Start()End Sub

Notice that after the thread is started, we do not inspect the square value of the class, because it is not guaranteed to have executed once you call the start method of the thread. There are a few ways to retrieve values back from another thread. The easiest way is to raise an event when the thread is complete. We will examine another method in the next section on thread synchronization. The following code adds the event declarations to the SquareClass.

Public Class SquareClass    Public Value As Double    Public Square As Double    Public Event ThreadComplete(ByVal Square As Double)    Public Sub CalcSquare()        Square = Value * Value        RaiseEvent ThreadComplete(Square)    End SubEnd Class

Catching the events in the calling code has not changed much from VB6, you still declare the variables WithEvents and handle the event in a procedure. The part that has changed is that you declare that a procedure handles the event using the Handles keyword and not through the naming convention of Object_Event as in VB6.

Dim WithEvents oSquare As SquareClassPrivate Sub Button1_Click(ByVal sender As System.Object, _        ByVal e As System.EventArgs) Handles Button1.Click    oSquare = New SquareClass()    t = New Thread(AddressOf oSquare.CalcSquare)    oSquare.Value = 30    t.Start()End SubSub SquareEventHandler(ByVal Square As Double) _        Handles oSquare.ThreadComplete    MsgBox("The square is " & Square)End Sub

The one thing to note with this method is that the procedure handling the event, in this case SquareEventHandler, will run within the thread that raised the event. It does not run within the thread from which the form is executing.



Synchronizing the Threads
VB.NET contains a few statements to provide synchronization of threads. In the Square example, you would want to synchronize the thread performing the calculation in order to wait for the calculation to complete so you can retrieve the result. Another example would be if you sort an array on a different thread and you would wait for that process to complete before using the array. To perform these synchronizations, VB.NET provides the SyncLockEnd SyncLock statement and the Thread.Join method.

SyncLock gains an exclusive lock to an object reference that is passed to it. By gaining this exclusive lock you can ensure that multiple threads are not accessing shared data or that the code is executing on multiple threads. A convenient object to use in order to gain a lock is the System.Type object associated with each class. The System.Type object can be retrieved using the GetType method:

Public Sub CalcSquare()    SyncLock GetType(SquareClass)        Square = Value * Value    End SyncLockEnd Sub

Lastly, the Thread.Join method allows you to wait for a specific amount of time until a thread has completed. If the thread completes before the timeout that you specify, Thread.Join returns True, otherwise it returns False. In the square sample, if we did not want to raise events, we could call the Thread.Join method to determine if the calculation has finished. The code would look like the following:

Private Sub Button1_Click(ByVal sender As System.Object, _        ByVal e As System.EventArgs) Handles Button1.Click    Dim oSquare As New SquareClass()    t = New Thread(AddressOf oSquare.CalcSquare)    oSquare.Value = 30    t.Start()    If t.Join(500) Then        MsgBox(oSquare.Square)    End IfEnd Sub

The one thing to note with this method is that the procedure handling the event, in this case SquareEventHandler, will run within the thread that raised the event. It does not run within the thread from which the form is executing.

devx-admin

devx-admin

Share the Post:
Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1

Copilot Revolution

Microsoft Copilot: A Suit of AI Features

Microsoft’s latest offering, Microsoft Copilot, aims to revolutionize the way we interact with technology. By integrating various AI capabilities, this all-in-one tool provides users with an improved experience that not

AI Girlfriend Craze

AI Girlfriend Craze Threatens Relationships

The surge in virtual AI girlfriends’ popularity is playing a role in the escalating issue of loneliness among young males, and this could have serious repercussions for America’s future. A

AIOps Innovations

Senser is Changing AIOps

Senser, an AIOps platform based in Tel Aviv, has introduced its groundbreaking AI-powered observability solution to support developers and operations teams in promptly pinpointing the root causes of service disruptions

Bebop Charging Stations

Check Out The New Bebob Battery Charging Stations

Bebob has introduced new 4- and 8-channel battery charging stations primarily aimed at rental companies, providing a convenient solution for clients with a large quantity of batteries. These wall-mountable and

Malyasian Networks

Malaysia’s Dual 5G Network Growth

On Wednesday, Malaysia’s Prime Minister Anwar Ibrahim announced the country’s plan to implement a dual 5G network strategy. This move is designed to achieve a more equitable incorporation of both

Advanced Drones Race

Pentagon’s Bold Race for Advanced Drones

The Pentagon has recently unveiled its ambitious strategy to acquire thousands of sophisticated drones within the next two years. This decision comes in response to Russia’s rapid utilization of airborne

Important Updates

You Need to See the New Microsoft Updates

Microsoft has recently announced a series of new features and updates across their applications, including Outlook, Microsoft Teams, and SharePoint. These new developments are centered around improving user experience, streamlining

Price Wars

Inside Hyundai and Kia’s Price Wars

South Korean automakers Hyundai and Kia are cutting the prices on a number of their electric vehicles (EVs) in response to growing price competition within the South Korean market. Many

Solar Frenzy Surprises

Solar Subsidy in Germany Causes Frenzy

In a shocking turn of events, the German national KfW bank was forced to discontinue its home solar power subsidy program for charging electric vehicles (EVs) after just one day,

Electric Spare

Electric Cars Ditch Spare Tires for Efficiency

Ira Newlander from West Los Angeles is thinking about trading in his old Ford Explorer for a contemporary hybrid or electric vehicle. However, he has observed that the majority of

Solar Geoengineering Impacts

Unraveling Solar Geoengineering’s Hidden Impacts

As we continue to face the repercussions of climate change, scientists and experts seek innovative ways to mitigate its impacts. Solar geoengineering (SG), a technique involving the distribution of aerosols

Razer Discount

Unbelievable Razer Blade 17 Discount

On September 24, 2023, it was reported that Razer, a popular brand in the premium gaming laptop industry, is offering an exceptional deal on their Razer Blade 17 model. Typically

Innovation Ignition

New Fintech Innovation Ignites Change

The fintech sector continues to attract substantial interest, as demonstrated by a dedicated fintech stage at a recent event featuring panel discussions and informal conversations with industry professionals. The gathering,

Import Easing

Easing Import Rules for Big Tech

India has chosen to ease its proposed restrictions on imports of laptops, tablets, and other IT hardware, allowing manufacturers like Apple Inc., HP Inc., and Dell Technologies Inc. more time

Semiconductor Stock Plummet

Dramatic Downturn in Semiconductor Stocks Looms

Recent events show that the S&P Semiconductors Select Industry Index seems to be experiencing a downturn, which could result in a decline in semiconductor stocks. Known as a key indicator

Anthropic Investment

Amazon’s Bold Anthropic Investment

On Monday, Amazon announced its plan to invest up to $4 billion in the AI firm Anthropic, acquiring a minority stake in the process. This decision demonstrates Amazon’s commitment to