Professional Documents
Culture Documents
VB Threads
VB Threads
VB Threads
The following are the important points which we need to remember about threads in visual basic.
In visual basic, the thread is a basic unit of execution within the process and it is responsible
for executing the application logic.
By default, every program will carry one thread to execute the program logic and that thread
is called the Main thread.
In visual basic, to work with threads we need to import System. Threading namespace in
our program.
After importing System. Threading namespace in our program, we can create or access the
threads using Thread class.
In visual basic, the Main thread will automatically create to handle the program logic. If we
create any other threads in our program by using Thread class those will
become child threads for the Main thread.
In visual basic, each thread will have a life cycle and it will start when we create an instance of
the object using Thread class.
In visual basic, by using Abort() method of Thread class we can end or abort the thread
execution process.
In visual basic, two types of threads are available, those are foreground threads and
background threads.
Foreground threads are the threads that will run until it finishes its work even if,
the Main thread completes its process.
Background threads will quit its process immediately when Main thread quits.
In visual basic, the thread is a basic unit of execution within the process and it is responsible for
executing the application logic.
By default, every application or program will carry one thread to execute the application logic and
that thread is called the Main thread. So, we can say that every program or application is by default
a single-threaded model.
For example, in windows operating system if we open Microsoft Excel and Word applications
simultaneously, then each application process will be taken care by separate threads.
In visual basic, we need to import System. Threading namespace in our program to work with
threads. Once we import System. Threading namespace, we can create or access the threads
using Thread class.
Generally, in visual basic when we start a program execution, the Main thread will automatically
create to handle the program logic. If we create any other threads in our program by
using Thread class those will become child threads for the Main thread.
Following is the example of getting the main thread details using Current Thread property
of Thread class.
Module Module1
Sub Main(ByVal args As String())
Dim t1 As Thread = Thread.CurrentThread
Console.WriteLine("Thread Id:{0}", t1.ManagedThreadId)
Console.WriteLine("Is Background Thread:{0}", t1.IsBackground)
Console.WriteLine("Thread Culture:{0}", t1.CurrentCulture)
Console.ReadLine()
End Sub
End Module
If you observe the above code, we imported System.Threading namespace to get the main thread
details by using CurrentThread property of Thread class.
When we execute the above visual basic program, we will get the result as shown below.
Thread Id:10
Is Background Thread:False
Thread Culture:en-IN
This is how we can access the main thread of our application which executing our program code
based on our requirements.
In visual basic, the Main thread is responsible for executing the programming logic in a synchronous
way that means one after another. In case, if we want to execute the few tasks simultaneously
(asynchronous way), then we need to create the child threads in our application.
Once the thread is created, we need to call the Start method of Thread class to start the execution
of the newly created thread.
As we discussed if we create any threads in our program by using Thread class those will
become child threads for the Main thread.
Following is the example of creating the thread in visual basic using Thread class.
Imports System.Threading
Module Module1
Sub Main(ByVal args As String())
' Create child thread
Dim t1 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo)
)
' Start newly created thread
t1.Start()
Console.WriteLine("Main Thread Completed")
End Sub
Private Sub PrintInfo()
For i As Integer = 1 To 4
Console.WriteLine("i value: {0}", i)
Next
Console.WriteLine("Child Thread Completed")
Console.ReadLine()
End Sub
End Module
When we execute the above program, we will get the result as shown below.
Property Description
CurrentContext It will return the current context in which the thread is executing.
CurrentCulture It is useful to get or set the culture for the current thread.
CurrentUICulture It is useful to get or set the Resource Manager current culture to look up culture-specific
resources at run time.
IsBackground It is useful to get or set a value which indicates whether the thread is background thread
or not.
IsThreadPoolThread It will return a value that indicates whether the thread belongs to the managed thread
Property Description
pool or not.
ManagedThreadId It will return the unique identifier of the currently managed thread.
Priority We can get or set a value that indicates the scheduling priority of a thread.
Method Description
BeginThreadAffinity() It will notify a host that the managed code is about to execute the instructions that
depend on the identity of the current physical operating system thread.
EndThreadAffinity() It will notify a host that the managed code has finished executing the instructions
that depend on the identity of the current physical operating system thread.
Method Description
Equals() It will determine whether the specified object is equal to the current object or not.
Finalize() It will ensure that the resources are freed and other cleanup operations are
performed when the garbage collector reclaims the Thread object.
GetData() It is useful to retrieve the value from the specified slot on the current thread.
GetDomain() It will return the current domain in which the current thread is running.
Join() It will make the thread to finish its work or it will halt other threads until it finishes
work.
Sleep() It will suspend the current thread for a specified amount of time.
Start() It will instruct the operating system to change the state of current instance to
Running.
VolatileRead() It will read the value of a field and that value is the latest written by any processor
in a computer.
Method Description
VolatileWrite() It will write a value to the field immediately so that the value will be visible for all
processors in the computer.
Yield() It will yield the execution to another thread that is ready to run on the current
processor.
At any point of time, the thread in visual basic will exists in any one of the following states.
State Description
Runnable When we call the Start() method, the thread will be moved to ready to run or runnable state.
Not If thread is in not runnable state means there is a chance that the Sleep() or Wait() or Suspend()
Runnable method is called on the thread or blocked by I/O operations.
Dead If thread is in dead state means the thread completes its task execution or it is aborted.
Following is the example of knowing the lifecycle and states of thread in visual basic.
Imports System.Threading
Module Module1
Sub Main(ByVal args As String())
' Create child thread
Dim t1 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo)
)
Console.WriteLine("Thread State: {0}", t1.ThreadState)
' Start newly created thread
t1.Start()
Console.WriteLine("Thread State: {0}", t1.ThreadState)
' Suspend thread
t1.Suspend()
Console.WriteLine("Thread State: {0}", t1.ThreadState)
' Resume thread to running state
t1.[Resume]()
Console.WriteLine("Thread State: {0}", t1.ThreadState)
Console.ReadLine()
End Sub
Private Sub PrintInfo()
Console.WriteLine("Method Execution")
End Sub
End Module
If you observe the above example, we used ThreadState property of Thread class to know the state
of thread and we used Start(), Suspend(), and Resume() methods of Thread class to start, suspend
and resume the execution of a thread.
When we execute the above program, we will get the result as shown below.