Introduction to Threading in Vb.net 05-24-2014, 03:33 PM
#1
Threading / MultiThreading [ Visualbasic.net Tutorial ]
In this tutorial we will learn about Threading in general for .net languages and then we will look at some examples of threading in vb.net.
The objectives of this tutorial are-
- To give you an idea of what Threading is.
- To give you an understanding of how Threading can be useful.
- To introduce you to Multi-Threading
- To give you ideas on how to use Threading in your projects
Introduction
Before we understand Threading itself , we need to acknowledge the fact that Threading is possible because the operating system
supports it.Strictly speaking , Microsoft Windows is a "pre-emptive multitasking operating system".All this means is that the
operating system has a task scheduler that gives a part of the processors time to each and every single running application.
This processor time is called as "time slices".
You must understand that it is not the program running that defines how much time slice it should get rather it is the task-scheduler that defines it.Now do you understand why a faster processor means better performance?
These time slices are very small , thus making it look like multiple processes are being carried out together.
Now that you have ate in some basic concepts , lets see the popular definition of a "thread".
A thread is a sequential flow of control.Every running program will at least have a single thread [ i.e. it can have more than
one threads ].
Now lets understand the difference between single threaded and multi-threaded applications.
![[Image: a8pwUyT.png]](http://i.imgur.com/a8pwUyT.png)
The above diagram sums up the main difference.In single threaded application each sub-routine or function is finished before proceeding to the next ; While in a multi-threaded application two or more subroutines or functions can run together.
Although multi-threading is a very complex but useful theory , you must not use it unless it is very much required because it can slow down your application and eat your processor.
Uses of Threading in Applications
Now we know what threading and multi-threading means. So now lets discuss where Threading is used in. Generally speaking ,
Threading is used in applications where two or more processes needs to be run together.
For eg- Source
- Keeping an UI active/responsive when an operation is being carried out
- Games where many calculations and drawings need to be made without interfering game play or FPS.
- FTP Client where each connection is treated individually
Threading in VisualBasic.net
Now that we know all about threading , lets create a simple multi-threading application in Visualbasic.net
Note : We are going to learn to create threads and use them programatically. However it is recommended that you also learn about the Backgroundworker component.Read Below for more Information.
Declaring A Thread
To create a thread , we need to have a process written ( in our case a sub-routine or a function )
Since we’re learning about multi-threading , we would need at least two sub’s.
I have created a new project with just a form , a multi-lined textbox and a button.
I’ve also added these two sub’s :
[
Code:
vbnet]
Sub Func1()
Dim i As Integer
For i = 1 To 5
TextBox1.AppendText("Sub1")
TextBox1.AppendText(vbNewLine)
Next
End Sub
Sub Func2()
Dim a As Integer
For a = 1 To 5
TextBox1.AppendText("Sub2")
TextBox1.AppendText(vbNewLine)
Next
End Sub
Firstly we’ll see how usual processing work. I’ve called both the subroutines in the button’s click event. Here’s the code:
[
Code:
vbnet]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Func1()
Func2()
End Sub
Code:
Sub1
Sub1
Sub1
Sub1
Sub1
Sub2
Sub2
Sub2
Sub2
Sub2
Its quite evident that Func2() has only been run after Func1() finished.
Now lets see what happens when we implement threads into this project.
Before that , you need to understand that in .Net only one thread should access a certain control else an unhandled exception will be thrown. So in essence , we cannot use the textbox to print the output this time because two threads will try to access the textbox at the same time.
Therefore , we will use debug.writeline() to print the function to the debug window(immediate window)
So I’ve modified the two sub-routines :
[
Code:
vbnet]Sub Func1()
Dim i As Integer
For i = 1 To 5
Debug.WriteLine("Sub1")
Next
End Sub
Sub Func2()
Dim a As Integer
For a = 1 To 5
Debug.WriteLine("Sub2")
Next
End Sub
And in the button’s call event :
We need to create two thread objects. To do that[The General Format] :
[
Code:
vbnet]
Dim Anyname as new system.threading.thread(AddressOf Subroutinename() )
[Shcode=vbnet]
Dim Thread1 As New System.Threading.Thread(AddressOf Func1)
Dim Thread2 As New System.Threading.Thread(AddressOf Func2)
[/code]
We need to start both the threads , so add:
[
Code:
vbnet] Thread1.start()
Thread2.start()
Code:
Sub2
Sub2
Sub2
Sub1
Sub2
Sub2
Sub1
Sub1
Sub1
Sub1
Using multi-threading can be quite hefty if it's not properly managed.For this reason , many programmers find it easier and performance-wise better to use Backgroundworker component.
I will try to make a tutorial on that too.
By this , we have reached the end of this tutorial. This is pretty much my first programming tutorial , so I'm very open to constructive criticism.
Last but not the least , let me suggest some further reads :
MSDN Library [ for Managed Threading ]
Multi Threading Article
Credits:-
Me for writing this , About.com , Visual Basic.NET in 60 minutes a Day.
Thank you for taking time to read my tutorial.
Visual Basic.Net
Private Message me for help
I'm not active because I'm busy with school.