Threads in VB.net
85Threads in VB.net
When we try to demonstrate Delegates in dot net, threads becomes the most effective example.You will see that in this hub. My intention is to explain why threads and how threads!
We live in this world and have our own life style. We do not do all things that the same time. We have lunch and do not work at that time. We will of course be busy gossiping while having lunch at office or at home. That apart, the point is we do not do all activities at the same time.
A computer is a machine just like a human body and needs rest. The overall concept of multiprocessing and multiprogramming has evolved with time. Threads is an example of multiprocessing. Due to multiprocessing the computer has turned out to be a more effective tool for business and for getting the required job done.Threads allow the computer to run various processes at the same time. With reference to Human life it is like we doing many activities at the same time but in breaks , giving and setting preferences.
Like Java, VB.net also supports threading and multithreading. It uses the namespace System.Threading.
A thread just like a delegate is declared using addressof due to which it becomes easy to call methods with specific arguments and return types declared after addressof.With delegate it becomes easy for us to run processes. For example if we have two methods to be executed ...with delegate it is possible to differentiate between two methods like one not knowing about the other ...running independently.
This is how a delegate using thread is declared
Declaring a thread
Dim thread1 as New Threading.Thread(AddressOf ourmethod) ' the thread will be assigned to the method ourmethod
It is time to concentrate on threads. A thread is a part of a process and also itself a process. The Thread class has interesting properties or methods. One of them is "Sleep". Sleep tells the thread when to pause in processing while other processes are asked to work. Sleep takes parameter as time in milliseconds for the period the process is to be suspended. We will see a simple Sleep example with the process being the main() of a vb dot net console application
Module Module1
Sub Main()
Dim i As Integer = 0
For i = 0 To 10
Console.WriteLine(i)
Threading.Thread.Sleep(5000)
Next
Console.WriteLine("main exits")
End Sub
End ModuleYou will find that the numbers 1 to 10 will print in a break of 5 seconds.1.2.3.4.and 5..(the number)
This will make the concept of suspending a process using Sleep of thread a bit clear
Vb dot net supports mulit threading and we can run methods or processes simultaneously using synchronization. Among various properties Join is used for synchronization to run two threads one after other. The system will choose between threads to run and this can be controlled using the Sleep class(simulation)
Module Module1
Sub Main()
Dim t As New tester
t.dotest()
Console.ReadLine()
End Sub
End Module
Class tester
Private counter As Integer
Public Sub dotest()
Dim t1 As New Threading.Thread(AddressOf incrementor)
t1.Name = "thread1" ' name to identify the thread
t1.Start() 'to start the thread
Console.WriteLine("starting thread ={0}", t1.Name)
Dim t2 As New Threading.Thread(AddressOf incrementor)
t2.Name = "thread2"
Console.WriteLine("starting thread ={0}", t2.Name)
t2.Start()
t1.Join()
t2.Join()
End Sub
Public Sub incrementor()
Try
While counter < 10
Dim temp As Integer = counter
temp = temp + 1
Threading.Thread.Sleep(1000) ' simulation
counter = temp
Console.WriteLine("thread :{0},value: {1}", Threading.Thread.CurrentThread.Name, counter)
End While
Catch ex As Exception
Finally
Console.WriteLine("we are done at {0}", Threading.Thread.CurrentThread.Name)
End Try
End Sub
End Class
You can see the output and check how the threads run simultaneously. The Finally in vb.net is a part of exception handling(in the code). This program tells us the use of Finally. The code written under Finally will execute whether or not exception occurs or not. The block in finally tells the current state after the code in try block has executed. Both Finally and Catch are optional
IsBackground and IsForeground thread
There is a difference between a background running thread and a foreground running thread.The foreground threads allow themselves to be shut down after the work has been accomplished while background threads are not capable of shutting down the processes. They will continue to run even after foreground threads have finished their work.
The following code will demonstrate it
' source MSDN
Imports System
Imports System.Threading
Public Class Test
Shared Sub Main()
Dim shortTest As New BackgroundTest(10)
Dim foregroundThread As New Thread(AddressOf shortTest.RunLoop)
foregroundThread.Name = "ForegroundThread"
Dim longTest As New BackgroundTest(50)
Dim backgroundThread As New Thread(AddressOf longTest.RunLoop)
backgroundThread.Name = "BackgroundThread"
backgroundThread.IsBackground = True
foregroundThread.Start()
backgroundThread.Start()
End Sub
End Class
Public Class BackgroundTest
Dim maxIterations As Integer
Sub New(maximumIterations As Integer)
maxIterations = maximumIterations
End Sub
Sub RunLoop()
Dim threadName As String = Thread.CurrentThread.Name
For i As Integer = 0 To maxIterations
Console.WriteLine("{0} count: {1}", _
threadName, i.ToString())
Thread.Sleep(250)
Next i
Console.WriteLine("{0} finished counting.", threadName)
End Sub
End Class
'the background thread will not be able to shut down





