Understanding Threads using Visual Basic 2005 - Part 1
page 2 of 7
by Abhishek Kumar Singh
Average Rating: 
Views (Total / Last 10 Days): 60404/ 103

Different ways to create threads in .NET

Using System.Threading namespace

Listing 1 – Create threads using System.Threading

Sub Main()
  Dim oThread As New System.Threading.Thread(New System.Threading.ThreadStart( _
                                             AddressOf MyThreadJob))
End Sub
Public Sub MyThreadJob()
  Console.WriteLine(Now.ToString() & " " & Now.TimeOfDay.ToString() & _
    " This is MyThreadJob")
End Sub
Public Sub MyThreadJob()
  Console.WriteLine(Now.ToString() & " " & Now.TimeOfDay.ToString())
End Sub

In this case the thread method can be either sub or function.

Using System.Windows.Forms.Timer namespace

We can use Windows.Forms.Timer object to create threads in each specified interval. Timer object accepts time interval value in milliseconds. Warning: Windows.Forms.Timer object may or may not work in applications which are not windows based. For example console application. Even if you add a reference to this namespace and your code compiles successfully, it may fail to create threads during execution. So we should avoid using Windows.Forms.Timer in non-form based applications. Use System.Threading.Thread instead.

Here is the sample code for a windows application to create threads using System.Windows.Forms.Timer

Listing 2 – Creating thread using System.Windows.Forms.Timer.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles MyBase.Load
        Dim oTimer As New System.Windows.Forms.Timer()
        AddHandler oTimer.Tick, AddressOf MyThreadJob
        oTimer.Interval = 5000
        Console.WriteLine(Now.ToString() & " " & Now.TimeOfDay.ToString() & "
        Starting Thread")
End Sub
Private Sub MyThreadJob(ByVal o As ObjectByVal e As EventArgs)
  MsgBox(Now.ToString() & " " & Now.TimeOfDay.ToString() & " This is MyThreadJob")
End Sub

Notice the signature of the thread method. In case of Windows.Forms.Timer the thread method must have Object and EventArgs types in its signature.

Using System.Threading.Timer namespace

Listing 3 – Creating thread using System.Threading.Timer

Sub Main()
  Dim oTimer As New System.Threading.Timer(New System.Threading.TimerCallback( _
    AddressOf MyThreadJob), Nothing, 0, 5000)
  Console.WriteLine(Now.ToString() & " " & Now.TimeOfDay.ToString() & _
    "  System.Threading.Timer configured")
End Sub
Private Sub MyThreadJob(ByVal s As Object)
  Console.WriteLine(Now.ToString() & " " & Now.TimeOfDay.ToString() & _
    " This is MyThreadJob")
End Sub

Notice the signature of the thread method. In case of System.Threading.Timer the thread method must have parameter of type Object. It is normally used to share state information between threads.

Using System.Timers.Timer

Listing 4 – Creating thread using System.Timers.Timer

Sub Main()
  Dim oTimer As New System.Timers.Timer(5000)
  AddHandler oTimer.Elapsed, AddressOf MyThreadJob
  oTimer.Enabled = True
End Sub
Private Sub MyThreadJob(ByVal s As ObjectByVal e As System.Timers.ElapsedEventArgs)
  Console.WriteLine(Now.ToString() & " " & Now.TimeOfDay.ToString() & _
    " This is MyThreadJob")
End Sub

Again, Notice the signature of the thread method MyThreadJob(). It must have Object and Timers.ElapsedEventArgs types in the signature.

Among four methodologies mentioned above, which we can use to build multithreaded applications, I would recommend going for the first procedure (for using System.Threading.Thread) always, unless we don't have any specific need of using others. Why? You can find differences among these in the real world through MSDN and forums.

At this moment you should know how to create threads in application. Each thread runs independently. Now your next task should be to control these independent threads in such a manner that they all can run as per our wish.

View Entire Article

User Comments

Title: Thread   
Name: Pari
Date: 2012-10-16 4:02:22 AM
very helpful
Title: Enginer   
Name: Didier Fonseca
Date: 2010-11-10 4:46:13 PM
Thanks,very good and easy to understand tutorial
Title: dig deeper   
Name: rs
Date: 2008-06-09 10:47:16 AM
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
thanks to all of you!
Title: Vey Good Article   
Name: Babita
Date: 2008-06-05 3:23:22 AM
Very useful article about thread.
Title: Good one   
Name: Soumya
Date: 2008-06-03 2:52:33 AM
Very helpful definitions and examples.
Title: nice article   
Name: rupesh
Date: 2008-06-03 2:40:32 AM
Very nice article about thread

Product Spotlight
Product Spotlight 

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

©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-08 2:39:15 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search