This article was previously published under Q315577
This article describes how to create threads in a Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 Windows application. The System.Threading namespace provides the classes that you must have to work with threads.
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
Microsoft Windows 2000 or Microsoft Windows XP
Microsoft Visual Basic .NET or Microsoft Visual Basic 2005
This article assumes that you are familiar with the following topics:
Visual Basic .NET programming or Visual Basic 2005 programming
Visual Studio .NET or Visual Studio 2005 Integrated Development Environment (IDE)
General familiarity with delegates and threading
General familiarity with Visual Basic .NET or Visual Basic 2005
Create a Visual Basic .NET or Visual Basic 2005 application with threads
Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
Create a new Visual Basic Windows Application project named ThreadWinApp.
Add a Button control to the form. The button is named Button1 by default.
Add a ProgressBar component to the form. The progress bar is named ProgressBar1 by default.
Right-click the form, and then click View Code.
Add the following statement to the beginning of the file:
Add the following Click event handler for Button1:
Private Sub Button1_Click( _ ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click MessageBox.Show("This is the main thread")End Sub
Add the following variable to the Form1 class:
Private trd As Thread
Add the following method to the Form1 class:
Private Sub ThreadTask() Dim stp As Integer Dim newval As Integer Dim rnd As New Random() Do stp = ProgressBar1.Step * rnd.Next(-1, 2) newval = ProgressBar1.Value + stp If newval > ProgressBar1.Maximum Then newval = ProgressBar1.Maximum ElseIf newval < ProgressBar1.Minimum Then newval = ProgressBar1.Minimum End If ProgressBar1.Value = newval Thread.Sleep(100) LoopEnd Sub
NOTE: This is the code that underlies the thread. This code is an infinite loop that randomly increments or decrements the value in ProgressBar1 and then waits 100 milliseconds before it continues.
Add the following Load event handler for Form1. This code creates a new thread, makes the thread a background thread, and then starts the thread.
Private Sub Form1_Load( _ ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load trd = New Thread(AddressOf ThreadTask) trd.IsBackground = True trd.Start()End Sub
Verify that it works
Build and run the application. Notice that the value in the progress bar changes randomly. This is the new thread in operation.
To demonstrate that the main thread is independent of the thread that changes the ProgressBar value, click the button on the form. A message box displays the following message:
This is the main thread
Wait for input. Notice that the value in the progress bar continues to change.
In more complex applications, make sure that you synchronize multiple threads when you access shared variables. For more information, refer to the SyncLock statement and related topics in the Visual Basic Online Help documentation.