|
|
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 |
Categories | VB.NET |
|
|
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.
|
|
Imports System.Threading
...
' 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 _
btnStartThread.Click
' 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 _
new_counter.Run)
' Make this a background thread so it automatically
' aborts when the main program stops.
counter_thread.IsBackground = True
' Start the thread.
counter_thread.Start()
End Sub
|
|
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
' window.
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 _
As Integer)
m_MyForm = my_form
m_Number = my_number
End Sub
' Count off seconds in the Output window.
Public Sub Run()
Try
Do
' Wait 1 second.
Thread.Sleep(1000)
' Lock the form object. This doesn't do
' anything
' to the form, it just means no other
' thread can
' lock the form object until we release the
' lock.
' That means a thread can update
' m_MyForm.Value
' and then display its value without
' interference.
SyncLock m_MyForm
' Increment the form's Value.
m_MyForm.Value += 1
' Write the value.
Debug.WriteLine(m_Number & ": " & _
m_MyForm.Value)
End SyncLock
Loop
Catch ex As Exception
' An unexpected error.
Debug.WriteLine("Unexpected error in thread " & _
_
m_Number & vbCrLf & ex.Message)
End Try
End Sub
End Class
|
|
|
|
|
|