Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / Visual Basic
Article

When a thread will terminate or dispose?

Rate me:
Please Sign up or sign in to vote.
2.95/5 (13 votes)
30 May 20053 min read 132K   1K   17   8
More information about threading and when a thread will dispose or will die.

Application interface

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.

VB
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
VB
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#.

VB
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.

VB
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:

VB
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
VB
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:

VB
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
VB
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:

VB
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
VB
sub case4()
 Dim MyThread As New Thread(AddressOf child)

 MyThread.Start()

 'abort and teriminate the new thread
 MyThread.Abort()
End Sub
VB
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!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Syrian Arab Republic Syrian Arab Republic
A Pharmacist Smile | :)

Comments and Discussions

 
PraiseThank you Pin
Imad AlFarouq Deeb19-Feb-19 21:41
Imad AlFarouq Deeb19-Feb-19 21:41 
GeneralMy vote of 1 Pin
Assassin_UKG21-Oct-13 23:21
Assassin_UKG21-Oct-13 23:21 
Questionhow to message box when a thread is completely end? Pin
stelios19849-Oct-13 3:22
stelios19849-Oct-13 3:22 
assume we have the following sub

private sub dowork
for i as integer=0 to 100
i=i
Next
messagebox.show("Thread is complete, but actually this message you see is executed from the child thread, How can you see it when actually this thread is completely end? and is no longer exists? e.g i want to see me in the main thread")
End Sub
QuestionPoints of Interest Pin
behos19-Jan-12 5:16
behos19-Jan-12 5:16 
Generalthis is very good article Pin
fadil19771-Jun-05 0:55
fadil19771-Jun-05 0:55 
GeneralInvoke Pin
RossB30-May-05 22:15
RossB30-May-05 22:15 
GeneralRe: Invoke Pin
smiling4ever31-May-05 2:57
smiling4ever31-May-05 2:57 
GeneralGood Pin
mhdhallak30-May-05 12:57
mhdhallak30-May-05 12:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.