Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Multithreading in VB.NET, A Beginner’s Guide

Author: Aadil
Download Source Code : 227_ThreadTest.zip

This simple article I will explain what is multithreading and how you can use it in a VB.NET application.

Whenever you develop a windows application using VB.NET and compile it to an executable and runs that executable in a windows environment you get a process.

I mean the software is run in its own process with its private memory and processor time. Usually the execution of the software is sequential that is all the program instructions are executed one after the other and there is where threads come into play. A thread is a logical sequence of program execution that is allocated processor time in itself. When a process runs it can spawn multiple threads of execution and each one can be independent of each other.

 

The capability of running multiple threads is new in VB.NET and previous versions of VB did not have this capability. This capability of creating multiple threads comes from the .NET Framework and a namespace called System.Threading is dedicated for threading operations. There is a Thread class that represents this logical entity.

 

When ever a program runs, by default a main thread is allocated for its execution and it is this thread that runs the whole process, unless you spawn (create) new threads from this main thread and this creation of multiple threads in a program is called Multithreading.

 

The need of Multithreading applications come into vision with special scenarios one of is if you need to do multiple operations concurrently. Let’s say you want to read a file from a network using Network Socket and also want the normal application to respond as usual then you might create a separate thread to handle the file read operation.

 

Another popular scenario is when you need to create a server application in which a server listens for requests from clients and each request is handled by a separate thread to fulfill and the server just hands out requests to a separate thread.

 

In the .NET Framework the Thread class is the main class to manage threads its default constructor a reference to a function of a special delegate called ThreadStart which is a function that takes no parameters and returns nothing. The execution in this function will be the execution of the new defining thread and that’s all for defining a simple execution thread.

 

To create multiple threads that execute concurrently just create multiple Thread objects and allocate a new function to execute as the thread’s execution code. When the thread execution function is defined in the constructor you need to call the Start method of the thread object to start its execution. There is also a method called Abort that permanently destroys the thread. There are methods to suspend and resume the thread.

 

To get a reference to the currently executing thread in a function you can just refer the Shared instance of the Thread class Thread.CurrentThread. So if you have a console application and you want to get the reference to the main startup thread just call Thread.CurrentThread and it will refer to this thread.

 

You can also rename a thread by changing its Name property. For further information on the Thread class please refer to the .NET Class Library reference. In the mean time I’ve created a simple example that you can work with to get yourself started with threads. It’s a console application and is written in VB.NET and its available for download.

Article Comments
I request you to help me i've to become a good .net programmer.

Let's no what are the prcedures i've to follow.

Posted on 3/27/2008 10:33:14 AM by Bhasheer Ahmed

Thanks Dude.. very helpfull to learn multithreading concept.

Posted on 11/8/2008 1:37:04 AM by Satish

It is helpfull to me. With the help of this i have clear
Multithreading Concept

Thank You Very Much!!!!!!!!

Posted on 11/24/2008 4:34:23 AM by Thakare

please Send me more samples on multithreading

Posted on 11/24/2008 4:35:50 AM by Thakare

Very good and simple explaination about multi-threading. I like and understand the concept. If u have some more complex code please send.

Posted on 2/2/2009 5:48:08 AM by Anil Sharma

Its very good example of code...please send some more adavnce example...
For example MY application for Call center Calling (Inbound)..
So i need it take records one -one by for caling to customers with some time lag say 5 sec.

Can we use the Threading concept for this.

Regards,
Virendra

Posted on 2/10/2010 4:49:47 AM by Virendra

Hi,
Thanks for your understandable article. Thanks to this article, i will use thread class easily more than before read.

would you please to share more articles concerned about multi-threading in VB.NET?

Thank you so much

Posted on 6/1/2010 4:12:52 AM by Aysegül

Please post other concepts of vb.net.
It will definitely help us.

Posted on 8/10/2010 3:26:59 PM by Sandeep

Your Article is Very Helpfull for threads !! thanks

Posted on 9/25/2010 1:36:27 PM by Vinay kumar Upadhyay

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Using Generics in ASP.NET 2.0 using VB.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net