How to create threads in Visual Basic .NET or Visual Basic 2005

Article translations Article translations
Article ID: 315577 - View products that this article applies to.
This article was previously published under Q315577
Expand all | Collapse all

On This Page

SUMMARY

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.

Requirements

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

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Create a new Visual Basic Windows Application project named ThreadWinApp.
  3. Add a Button control to the form. The button is named Button1 by default.
  4. Add a ProgressBar component to the form. The progress bar is named ProgressBar1 by default.
  5. Right-click the form, and then click View Code.
  6. Add the following statement to the beginning of the file:
    Imports System.Threading
    					
  7. 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
    					
  8. Add the following variable to the Form1 class:
    Private trd As Thread
    					
  9. 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)
        Loop
    End 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.
  10. 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

  1. Build and run the application. Notice that the value in the progress bar changes randomly. This is the new thread in operation.
  2. 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.

Troubleshooting

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.

Properties

Article ID: 315577 - Last Review: December 6, 2006 - Revision: 4.3
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005swept kbvs2005applies kbhowtomaster KB315577

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com