Understanding Threads using Visual Basic 2005 - Part 1
page 4 of 7
by Abhishek Kumar Singh
Feedback
Average Rating: 
Views (Total / Last 10 Days): 51918/ 57

Implementing Sleep() and Join() methods in Threading

Create a console application and update the code in module as given below.

Listing 5 – Implementing thread creation without using Sleep and Join methods

Module Module1
 
Sub Main()
  Console.WriteLine("{0} : {1} : Main thread started...", _
    Threading.Thread.CurrentThread.ManagedThreadId, Now.ToString())
 
  Dim oThOneMethod As Threading.ThreadStart = _
    New Threading.ThreadStart(AddressOf DoAction)
  Dim oThTwoMethod As Threading.ThreadStart = _
    New Threading.ThreadStart(AddressOf DoAction)
 
  Dim oTh1 As Threading.Thread = New Threading.Thread(oThOneMethod)
  Dim oTh2 As Threading.Thread = New Threading.Thread(oThTwoMethod)
 
  oTh1.Start()
  oTh2.Start()
 
  Console.WriteLine("{0} : {1} : Main thread finishing...", _
                    Threading.Thread.CurrentThread.ManagedThreadId, Now.ToString())
End Sub
 
Public Sub DoAction()
  Console.WriteLine("{0} : {1} : To sleep now...", _
    Threading.Thread.CurrentThread.ManagedThreadId, Now.ToString())
  Threading.Thread.Sleep(5000)
  Console.WriteLine("{0} : {1} : is woke up...", _
    Threading.Thread.CurrentThread.ManagedThreadId, Now.ToString())
End Sub
End Module

** [Please note that you should use CTRL+F5 key combination to run the application with code provided in this article since I have not used Console.Read() at the end of Main(). CTRL+F5 will cause the console to wait for any key press at the end. This way you can see the output in the console window. Otherwise you can add Console.Read() in main().]

If you run this application (CTRL+F5), you will get output in console window a given below.

Figure 1 – Console output of application

Does the result seem proper? No, because in general we don’t want to stop the main thread before finishing generated threads. Here is what we need to use Join() method which will wait till new threads finish. Modify the Main() method to use Join() as given below. I have only added oTh1.Join() and oTh2.Join() in the Main() method.

Listing 6 – Implementing thread creation using Sleep and Join methods

Sub Main()
  Console.WriteLine("{0} : {1} : Main thread started...", _
    Threading.Thread.CurrentThread.ManagedThreadId, Now.ToString())
 
  Dim oThOneMethod As Threading.ThreadStart = _
    New Threading.ThreadStart(AddressOf DoAction)
  Dim oThTwoMethod As Threading.ThreadStart = _
    New Threading.ThreadStart(AddressOf DoAction)
 
  Dim oTh1 As Threading.Thread = New Threading.Thread(oThOneMethod)
  Dim oTh2 As Threading.Thread = New Threading.Thread(oThTwoMethod)
 
  oTh1.Start()
  oTh2.Start()
 
  oTh1.Join()
  oTh2.Join()
 
  Console.WriteLine("{0} : {1} : Main thread finishing...", _
                    Threading.Thread.CurrentThread.ManagedThreadId, Now.ToString())
End Sub

Now run the application (CTRL+F5), you should see following console output.

Figure 2 – Console output of the application

This time result is as we wanted. New threads are finished before the main thread.


View Entire Article

User Comments

Title: Thread   
Name: Pari
Date: 2012-10-16 4:02:22 AM
Comment:
very helpful
Title: Enginer   
Name: Didier Fonseca
Date: 2010-11-10 4:46:13 PM
Comment:
Thanks,very good and easy to understand tutorial
Title: dig deeper   
Name: rs
Date: 2008-06-09 10:47:16 AM
Comment:
nice introduction in synchronization. next thing that comes up comparing different solutions is rating them. how do they differ (e.g. in performance) and which is suitable for what kind of problem ...
Title: thanks   
Name: Abhishek Singh
Date: 2008-06-06 6:50:45 AM
Comment:
thanks to all of you!
Title: Vey Good Article   
Name: Babita
Date: 2008-06-05 3:23:22 AM
Comment:
Very useful article about thread.
Title: Good one   
Name: Soumya
Date: 2008-06-03 2:52:33 AM
Comment:
Very helpful definitions and examples.
Title: nice article   
Name: rupesh
Date: 2008-06-03 2:40:32 AM
Comment:
Very nice article about thread

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 7:17:11 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search