When a thread will terminate or dispose?






2.95/5 (13 votes)
May 30, 2005
3 min read

132958

1017
More information about threading and when a thread will dispose or will die.
Introduction
The purpose of this article is to discuss and to explain the ways in which threading will go out of scope, in other words, thread death. This article does not teach or guide you on how to use or write threads. (See background.)
Without any doubt, multi-threading application programming is one of the hardest topics and a more complex one in programming. As threads increase in an application it would be difficult to manage and execute them. I'll discuss the whole life of a thread from initiating it to disposing (death).
Background
To read this article, you have to have a good knowledge of the Threading
namespace and how to use threading and in what condition it is useful. Scott Bonds published an article Introduction to making multithreaded VB.NET Apps. It is highly recommended that you refer to it before proceeding through this article. Another good article is Threading in VB.NET.
Using the code
I attached the source code which will explain the topic, and below is some of the explanation.
The Main_Load
sub procedure that handles the form load event has the following code which will make a main thread that will execute while the application is running to show you that different threads are being executed. Note that this thread has the Priority set to below normal to give chance for other threads to run.
Private Sub Main_Load (ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles MyBase.Load
'Make an instance of the Thread addressing it to
'the procedure that will execute in response
Dim MyThread As New Thread(AddressOf MainThread)
MyThread.Priority = ThreadPriority.BelowNormal
'start the new thread
MyThread.Start()
End Sub
Sub MainThread()
For counter As Long = 1 To 100000000
txtMainThread.Text = counter
Application.DoEvents()
Next
End Sub
Before starting, let's instantiate the thread and the procedure that will handle that thread (will be executed). The variable MyThread
will hold the thread which has the delegate Case1
.
Remember: You don't have to declare a delegate for the procedure you want to use for threading in VB.NET, it will be done behind the scenes in contrast to C#.
Private Sub btnStartThread1_Click( ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles btnStartThread1.Click
'Make an instance of the Thread addressing it to
'the procedure that will execute in response
Dim MyThread As New Thread(AddressOf Case1)
'start the new thread
MyThread.Start()
End Sub
The following is the sub procedure that will execute when the thread starts. A For ... Next
loop is located inside it and will generate a counter which will be displayed in a textbox named txtMyNewThread
.
Sub Case1()
For counter As Integer = 1 To 100000
txtMyNewThread.Text = counter
application.DoEvents()
Next
End Sub
Case I (When the Thread's sub procedure ENDS)
As you can see in the proceeding code, the sub procedure Case1
will end when the executing code (For ... Next
statement) ends. In other words, reaching the statement End Sub
will terminate this sub procedure and also will terminate the thread.
Case II (Using the Exit Sub command inside the sub procedure)
Using the Exit Sub
command inside the procedure will terminate the procedure and hence the thread as shown below:
Private Sub btnStartThread2_Click( ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles btnStartThread1.Click
'Make an instance of the Thread addressing it to
'the procedure that will execute in response
Dim MyThread As New Thread(AddressOf Case2)
'start the new thread
MyThread.Start()
End Sub
sub Case2()
For counter As Integer = 1 To 100000
txtMyNewThread.Text = counter
If counter = 5000 Then Exit Sub
application.DoEvents()
Next
End Sub
Case III (Calling abort() method from the thread itself)
Every thread could be terminated by calling a method named abort()
from the thread procedure or another sub procedure that branched from it. An example is shown below:
Private Sub btnStartThread3_Click( ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles btnStartThread1.Click
'Make an instance of the Thread addressing it to
'the procedure that will execute in response
Dim MyThread As New Thread(AddressOf Case3)
'start the new thread
MyThread.Start()
End Sub
sub Case3()
For counter As Integer = 1 To 100000
txtMyNewThread.Text = counter
If counter = 5000 Then Thread.CurrentThread.Abort()
application.DoEvents()
Next
End Sub
Case IV (Terminating from Parent Thread that executed the Child thread)
The thread (parent) that executed the new thread (child) terminates the child thread as shown below:
Private Sub btnStartThread4_Click( ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles btnStartThread1.Click
'Make an instance of the Thread addressing it to
'the procedure that will execute in response
Dim MyThread As New Thread(AddressOf Case4)
'start the new thread
MyThread.Start()
End Sub
sub case4()
Dim MyThread As New Thread(AddressOf child)
MyThread.Start()
'abort and teriminate the new thread
MyThread.Abort()
End Sub
sub child()
For counter As Integer = 1 To 100000
txtMyNewThread.Text = counter
Application.DoEvents()
Next
End Sub
Points of Interest
While running the code, I was interested when I opened the Task Manager (Alt + Ctrl + Del) - Performance tab to see processor loads in percentage and threads being executed.
Finally
I guess that I didn't mention some of the other cases that will terminate the thread as well. Anyway, I'll try to mention them later on.
Please show me your feedback.
Thank you!