|
|
Title | Run Threads with different priorities in VB.NET |
Description | This example shows how to run Threads with different priorities in VB.NET. |
Keywords | thread, multi-threading, SyncLock, asynchronous, threading, priority |
Categories | VB.NET |
|
|
This program manages three Threads with high, normal, and low priorities.
When the user clicks a button, the program uses the MakeThread method to start an appropriate thread. When the user clicks the Start All Threads button, it uses MakeThread to create and run all three kinds of thread.
Subroutine MakeThread checks a Thread variable to see if we have used that Thread before. If we have used the Thread, it uses the Thread's IsAlive method to see if it is still running. If the Thread is still running, then the subroutine does nothing else.
If there is no running thread of this priority, the program creates one and starts it.
|
|
Imports System.Threading
...
' The threads.
Private m_HighThread As Thread
Private m_NormalThread As Thread
Private m_LowThread As Thread
Private Sub btnHigh_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnHigh.Click
MakeThread(m_HighThread, "High", _
ThreadPriority.AboveNormal)
End Sub
Private Sub btnNormal_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnNormal.Click
MakeThread(m_NormalThread, "Normal", _
ThreadPriority.Normal)
End Sub
Private Sub btnLow_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLow.Click
MakeThread(m_LowThread, "Low", _
ThreadPriority.BelowNormal)
End Sub
Private Sub btnAll_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAll.Click
MakeThread(m_LowThread, "Low", _
ThreadPriority.BelowNormal)
MakeThread(m_NormalThread, "Normal", _
ThreadPriority.Normal)
MakeThread(m_HighThread, "High", _
ThreadPriority.AboveNormal)
End Sub
' Make a thread with the indicated priority.
Private Sub MakeThread(ByRef thread_variable As Thread, _
ByVal thread_name As String, ByVal thread_priority As _
ThreadPriority)
' See if the thread is already running.
Dim is_running As Boolean
If thread_variable Is Nothing Then
is_running = False
Else
is_running = thread_variable.IsAlive
End If
' If the thread is already running, do nothing.
If is_running Then Exit Sub
' Make the thread.
Dim new_counter As New Counter(thread_name)
thread_variable = New Thread(AddressOf new_counter.Run)
thread_variable.Priority = thread_priority
thread_variable.IsBackground = True
thread_variable.Name = thread_name
' Start the thread.
thread_variable.Start()
End Sub
|
|
Each Thread executs a Counter object's Run method. That method loops 10 times. Each time it displays the object's name (High, Normal, or Low) and the object's count.
Next the routine wastes half a second. It does this in a loop without sleeping or calling DoEvents so it doesn't give up control of the CPU.
As the operating system sees fit, it swaps between the different Threads. When you click the Start All Threads button, you will see that the high priority Thread gets the most CPU time, the normal priority Thread gets the second most, and the Low priority Thread gets the least.
|
|
' This class's Run method displays a count in the Output
' window.
Public Class Counter
' This counter's number.
Private m_Name As String
' The iteration count.
Private m_Count As Integer = 0
Public Sub New(ByVal my_number As String)
m_Name = my_number
End Sub
' Count off 10 half second intervals in the Output
' window.
Public Sub Run()
For i As Integer = 1 To 10
' Display the next message.
Debug.WriteLine(m_Name & " " & m_Count)
m_Count += 1
' See when we should display the next message.
Dim next_time As DateTime = Now.AddSeconds(0.5)
' Waste half a second.
' Note that we do not sleep, call DoEvents,
' or otherwise give up control of the CPU.
Do While Now < next_time
' Wait a bit.
Loop
Next i
End Sub
End Class
|
|
|
|
|
|