|
Multithreading
Suspending a Thread
Threads can be suspended. Suspending a thread stops it temporarily. Working with the
example in the previous section, add a new button Button2 to the main form. When this
button is clicked the thread is suspended. The code for that looks like this:
Private Sub Button2_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button2.Click
Thread1.Suspend()
End Sub
|
Resuming a Thread
Threads can be resumed after they are suspended. With the example above, add a new
button Button3 to the main form. When this button is clicked the thread is resumed
from suspension. The code for that looks like this:
Private Sub Button3_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button3.Click
Thread1.Resume()
End Sub
|
Making a Thread Sleep
Threads can be made to sleep which means that they can be suspended over a specific
period of time. Sleeping a thread is achieved by passing the time (in milliseconds,1/1000
of a second) to the thread's sleep method. With the example above, add a new button
Button4 to the main form. When this button is clicked the thread is stopped. The code
for that looks like this:
Private Sub Button4_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button4.Click
Thread1.Sleep(100/1000)
End Sub
|
Stopping a Thread
Threads can be stopped with it's abort method. With the example above, add a new button
Button5 to the main form. When this button is clicked the thread is stopped. The code
for that looks like this:
Private Sub Button5_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button5.Click
Thread1.Abort()
End Sub
|
Thread Priorities
Threads can also be assigned priority for execution. Thread priority can be set by
the thread's Priority property and assigning a value from predefined Thread Priority
enumeration.
Values for Thread Priority:
Above Normal -> Gives thread higher priority
Below Normal ->Gives thread lower priority
Normal -> Gives thread normal priority
Lowest -> Gives thread lowest priority
Highest -> Gives thread highest priority
Working with the above example, add a new button Button6 to the main form. When this
button is clicked the thread is assigned Highest priority .The code for that looks
like this:
Private Sub Button6_Click(ByVal sender as System.Object, ByVal e as System.EventArgs)_
Handles Button6.Click
Thread1.Priority=System.Threading.ThreadPriority.Highest
'setting Highest priority for the thread
End Sub
|
For more information on Threads please refer to MSDN.
|