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))
oThread.Start()
Console.Read()
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")
oTimer.Start()
End Sub
Private Sub MyThreadJob(ByVal o As Object, ByVal 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")
Console.Read()
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
Console.Read()
End Sub
Private Sub MyThreadJob(ByVal s As Object, ByVal 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.