Microsoft KB Archive/315577

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

Article ID: 315577

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q315577





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
 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: Imports System.Threading   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(&quot;This is the main thread&quot;) End Sub </li>  Add the following variable to the Form1 class: Private trd As Thread </li>  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. </li>  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 </li></ol>

Verify that it works
<ol> Build and run the application. Notice that the value in the progress bar changes randomly. This is the new thread in operation.</li> 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:

<pre class="fixed_text">This is the main thread

Wait for input. Notice that the value in the progress bar continues to change.</li></ol>

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.

Additional query words: multi-threaded

Keywords: kbvs2005swept kbvs2005applies kbhowtomaster KB315577

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.