Microsoft KB Archive/315677

= HOW TO: Create Threads in Visual Basic .NET =

PSS ID Number: 315677

Article Last Modified on 7/30/2003

-

The information in this article applies to:


 * Microsoft Visual Basic .NET (2002)
 * Microsoft Visual Basic .NET (2003)
 * Microsoft Visual Studio .NET (2002), Professional Edition

-



This article was previously published under Q315677





IN THIS TASK

 * SUMMARY
 * ** Requirements
 * Creating Threads
 * Verification
 * Complete Code Listing (Form1.vb)
 * Troubleshooting
 * REFERENCES



SUMMARY
This article describes how to create threads in a Visual Basic .NET Windows application. The System.Threading namespace provides the classes that are needed to work with threads.

back to the top

Requirements
The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that you need:
 * Microsoft Visual Studio .NET

Prior knowledge required:
 * General familiarity with delegates and threading
 * General familiarity with Microsoft Visual Basic .NET

back to the top

Creating Threads
To create a Windows application that uses Visual Basic .NET and that demonstrates the creation of threads, follow these steps:  Open Microsoft Visual Studio .NET, and then click New Project. Create a new Windows application named ThreadDemo in Visual Basic .NET. Verify that the Solution Explorer is visible. If it is not, press CTRL+ALT+L. Verify that Form1.vb is open in the editor window. If Form1.vb is not open, double-click Form1.vb in the Solution Explorer to open it. To open the Toolbox, press CTRL+ALT+X. In the ToolBox, click the Windows Forms tab. Select and drag a button to the form. Press F4 to display the Properties window. Set the Text property of the button to Start New Thread. Press CTRL+SHIFT+A to open the Add New Item dialog box. Select Windows Form from the list of templates, and then name the item Form2.vb. Click OK.</li> From the ToolBox, drag a label to the form. Set the Text Property to Hello, I'm your new thread. Drag a button under the label. Set its Text Property to OK, now put me to sleep!</li> Double-click Form2. Your insertion point should now be in a new Form2_Load event handler. With your insertion point on the first line of the event handler, press F9 to set a breakpoint.</li>  Add an Imports statement above the Class declaration to include the classes that reside in the System.Threading namespace: Imports System.Threading </li>  Click the Form2.vb (Design) tab above the editor window to return to Design view. Double-click the button to create a Click event handler in Form2.vb. Add the following code, so that the event handler reads: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Hide Thread.CurrentThread.Sleep(2000) MessageBox.Show(&quot;Thanks, I needed the rest.&quot;, MessageBoxButtons.OK) End Sub </li>  Select the Form1.vb (Design) tab. Double-click the form to create the Form_Load event. Set a breakpoint on the first line of Form_Load. Add the following Imports statement above the class declaration: Imports System.Threading Add a Start subroutine after the Form_Load method. This is the entry point for new threads in the application: Private Sub Start Dim myForm2 As New Form2 myForm2.ShowDialog End Sub </li>  Select the Form1.vb (Design) tab. Double-click the Start New Thread button to create the event handler. This handler contains the code to create a ThreadStart delegate at the address of the Start subroutine: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim NewThreadStart As New ThreadStart(AddressOf Me.Start) Dim NewThread As New Thread(NewThreadStart) NewThread.Start End Sub </li></ol>

back to the top

Verification

 * 1) Press F5 to run the application in debug mode. The application should break at the Form1_Load event.
 * 2) Press CTRL+ALT+H to open the Threads window in the debugger. Notice a single thread at location &quot;Threading.Form1.Form1_Load&quot;.
 * 3) Press F5 to continue. Form1 should appear. Click Start New Thread.
 * 4) The application should break at the Form2_Load event. Notice that a second thread appears.
 * 5) Press F5 to continue. Form2 should appear. Click OK, now put me to sleep! After two seconds, the message box should appear.
 * 6) Click OK to clear the message box.

back to the top

Complete Code Listing (Form1.vb)
Imports System.Threading

Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

Public Sub New MyBase.New

'This call is required by the Windows Form Designer. InitializeComponent

'Add any initialization after the InitializeComponent call.

End Sub

'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub Friend WithEvents Button1 As System.Windows.Forms.Button 'Required by the Windows Form Designer. Private components As System.ComponentModel.Container

'NOTE: The following procedure is required by Windows Form Designer. 'It can be modified by using the Windows Form Designer. 'Do not modify it by using the code editor. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout '       'Button1 '       Me.Button1.Location = New System.Drawing.Point(80, 24) Me.Button1.Name = &quot;Button1&quot; Me.Button1.Size = New System.Drawing.Size(120, 23) Me.Button1.TabIndex = 0 Me.Button1.Text = &quot;Start New Thread&quot; '       'Form1 '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 70) Me.Controls.AddRange(New System.Windows.Forms.Control {Me.Button1}) Me.Name = &quot;Form1&quot; Me.Text = &quot;Form1&quot; Me.ResumeLayout(False)

End Sub


 * 1) End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Start Dim myForm2 As New Form2 myForm2.ShowDialog End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim NewThreadStart As New ThreadStart(AddressOf Me.Start) Dim NewThread As New Thread(NewThreadStart) NewThread.Start End Sub

End Class

Complete Code Listing (Form2.vb) Imports System.Threading

Public Class Form2 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

Public Sub New MyBase.New

'This call is required by the Windows Form Designer. InitializeComponent

'Add any initialization after the InitializeComponent call.

End Sub

'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label

'Required by the Windows Form Designer. Private components As System.ComponentModel.Container

'NOTE: The following procedure is required by Windows Form Designer. 'It can be modified by using the Windows Form Designer. 'Do not modify it by using the code editor. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent Me.Label1 = New System.Windows.Forms.Label Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout '       'Label1 '       Me.Label1.Font = New System.Drawing.Font(&quot;Microsoft Sans Serif&quot;, 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(80, 32) Me.Label1.Name = &quot;Label1&quot; Me.Label1.Size = New System.Drawing.Size(144, 24) Me.Label1.TabIndex = 1 Me.Label1.Text = &quot;Hello, I'm your new thread.&quot; '       'Button1 '       Me.Button1.Location = New System.Drawing.Point(80, 64) Me.Button1.Name = &quot;Button1&quot; Me.Button1.Size = New System.Drawing.Size(128, 40) Me.Button1.TabIndex = 0 Me.Button1.Text = &quot;OK, now put me to sleep!&quot; '       'Form2 '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 134) Me.Controls.AddRange(New System.Windows.Forms.Control {Me.Label1, Me.Button1}) Me.Name = &quot;Form2&quot; Me.Text = &quot;Form2&quot; Me.ResumeLayout(False)

End Sub


 * 1) End Region

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Hide Thread.CurrentThread.Sleep(2000) MessageBox.Show(&quot;Thanks, I needed the rest.&quot;, MessageBoxButtons.OK) End Sub End Class back to the top

Troubleshooting
Spawning threads is relatively simple; however, writing true multithreaded applications is quite complicated. Before you write multithreaded applications, you should be aware of thread synchronization issues.

back to the top