Click here to Skip to main content
Click here to Skip to main content

When a thread will terminate or dispose?

, 30 May 2005
Rate this:
Please Sign up or sign in to vote.
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.

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!

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

Share

About the Author

smiling4ever
Web Developer
Syrian Arab Republic Syrian Arab Republic
A Pharmacist Smile | :)

Comments and Discussions

 
GeneralMy vote of 1 PinmemberAssassin_UKG21-Oct-13 23:21 
Questionhow to message box when a thread is completely end? Pinmemberstelios19849-Oct-13 3:22 
QuestionPoints of Interest Pinmemberbehos19-Jan-12 5:16 
Generalthis is very good article Pinmemberfadil19771-Jun-05 0:55 
GeneralInvoke PinmemberRossB30-May-05 22:15 
GeneralRe: Invoke Pinmembersmiling4ever31-May-05 2:57 
GeneralGood Pinmembermhdhallak30-May-05 12:57 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 30 May 2005
Article Copyright 2005 by smiling4ever
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid