|Title||Start multiple threads in VB.NET|
|Description||This example shows how to start multiple threads in VB.NET.|
|Keywords||thread, multi-threading, SyncLock, asynchronous, threading|
When the user clicks the Start Thread button, the program creates a new Counter object. It then creates a Thread, initializing it to execute the Counter's Run method.
The program sets the Thread's IsBackground property to True so it will automatically stop when the program's main thread stops. If you don't do this, the Thread will keep running until its Run method exits (which it never does so it would run until it reached an overflow problem).
The program calls the Thread's Start method to make it run. The user can click this button several times to start different Threads.
' This value is incremented by all threads.
Public Value As Integer = 0
Private Sub btnStartThread_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
' Make a new counter object.
Static thread_num As Integer = 0
Dim new_counter As New Counter(Me, thread_num)
thread_num += 1
' Make a thread to run the object's Run method.
Dim counter_thread As New Thread(AddressOf _
' Make this a background thread so it automatically
' aborts when the main program stops.
counter_thread.IsBackground = True
' Start the thread.
A Counter object displays its name and a count in the Debug window. The count comes from the form's Value variable.
The Run method enters an infinite loop. It sleeps for 1 second and then uses a SyncLock statement to get a lock on the form. This doesn't do anything to the form, it just means no other thread can lock the form object until we release the lock. To see why this is important, suppose the form's Value variable holds the value 1. Then suppose two threads are running and both enter this section of code at approximately the same time and follow this sequence of events:
- Object 1 increments the form's Value property to 2.
- Object 2 increments the form's Value property to 3.
- Object 1 displays the form's Value property 3.
- Object 2 displays the form's Value property 3.
Instead of displaying the value 2 and then 3, both of the threads display the value 3.
After it locks the form object, the Counter increments Value and displays the new value in the Output window. It then uses the End SyncLock statement to release its lock.
If another thread reaches its SyncLock statement while this object holds a lock on the form, the second object waits until the lock is released.
' This class's Run method displays a count in the Output
Public Class Counter
' The form that owns the Value variable.
Private m_MyForm As Form1
' This counter's number.
Private m_Number As Integer
Public Sub New(ByVal my_form As Form1, ByVal my_number _
m_MyForm = my_form
m_Number = my_number
' Count off seconds in the Output window.
Public Sub Run()
' Wait 1 second.
' Lock the form object. This doesn't do
' to the form, it just means no other
' thread can
' lock the form object until we release the
' That means a thread can update
' and then display its value without
' Increment the form's Value.
m_MyForm.Value += 1
' Write the value.
Debug.WriteLine(m_Number & ": " & _
Catch ex As Exception
' An unexpected error.
Debug.WriteLine("Unexpected error in thread " & _
m_Number & vbCrLf & ex.Message)