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
 

Threading using VB.Net 2005

Author: Faraz
Download Source Code : 249_ThreadApplication.zip

In this article I will explain you how you can create threads using Visual Basic.Net 2005. The class use for threading is System.Threading.

Threading is one of the interesting features provided in .Net Framework 1.0 and .Net Framework 2.0. Threads are the basic unit to which an operating system allocates processors time. Where as, each process can have more than one thread. Each thread contains information related to exception handlers, a scheduling priority, and a structure in which thread context is saved.

 

Threads provide the ability to an operating to run multiple applications in way that it seams that the applications are running simultaneously. But for the processor each application is running one by one according to their allocated schedule or priority. This scheduling of threads is known as Multithreading and this task of scheduling or priority setting is done by thread scheduler.

 

We can define multithreading as, the ability to create more than one thread inside a process. These threads serve as a multiple execution points that enable different segments of your application to run at the same time.

 

As I have mentioned earlier that for threading purposes in .Net Framework 1.0 and .Net Framework 2.0 we have System.Threading class that provides the facility for the user to use methods by creating threading objects.

 

Syntax for creating a thread object is as follows:

 

Dim nthread As Thread

When the thread object is created you need to pass the pointer to the method delegate:

nthread = New Thread(AddressOf CreateThread)

Here “CreateThread” is a thread delegate. After this you need a Start() Method to start the thread execution:

nthread.Start()

Start() method is responsible for actually spinning up the thread for you. Once the thread is created you can access the thread name, its ID, status, priority, and many other properties using thread object.

Here I have created a simple console application in Visual Basic.Net using .Net Framework 2.0. The code for this application is illustrated below (you can also download this code):

 

Imports System.Threading

 

Module Module1

 

      Sub Main()

 

            Dim nthread As Thread

 

            For i As Integer = 1 To 10

                  nthread = New Thread(AddressOf CreateThread)

                  nthread.Name = "CTH :" & i

                  nthread.Start()

            Next

      End Sub

 

      Private Sub CreateThread()

            Dim j As Integer

            For j = 1 To 5

                  Console.WriteLine(Thread.CurrentThread.Name)

                  Thread.CurrentThread.Sleep(1000)

            Next

            Console.ReadLine()

      End Sub

 

End Module

In the above code first of all I have create a thread object, then I assign a thread delegate to this thread object. After this I set the thread name and finally using the Start() method I start the execution of thread.

CreateThread() method creates a single thread and wait for 1 second. In a mean while, the second thread starts its execution, and this process continues until all the 10 threads are created. When the 10th thread goes in sleep state the 1st thread again starts its execution and again the same process is repeated. This process continues until all the threads terminates.

Article Comments
Great explaination and sample.
Thank You

Posted on 2/11/2007 7:00:25 AM by Phil

Useful ....

Posted on 1/16/2008 6:09:27 AM by RXYH

concrete and concise detail for beginner.
I appreciate.

Posted on 2/14/2008 4:14:05 AM by Kashif Hussain

easy to understand but must include features of threads

Posted on 2/27/2008 8:55:13 AM by Vijay Ahire

Hey Friend,
Thanks for such a nice example
i learnt manu things from this examples
thanks thanks a lot and i hope that you will give me more about threading in .net

Posted on 2/29/2008 6:30:57 AM by Niral Patel

Thank u as simple as java

Posted on 3/13/2008 5:29:58 AM by kanchan

Good Example For Beginner

Posted on 3/15/2008 8:10:43 AM by Nishant

Questo esempio lo usa mia figlio all'asilo.

Posted on 3/27/2008 3:30:06 AM by Max

Realy Nice..I like it!

Posted on 4/21/2008 4:02:07 PM by zeeshan

what are doing of delegate in vb.nte2005
(uses of delegate)

Posted on 7/10/2008 3:56:25 AM by ali ahmad

hii
thanks for giving such a nice code..easy to understand
but i want to use it in windows apllication for some purpose..
plz tell me how to use it in wiondows application

Posted on 12/19/2009 1:25:38 AM by mamta

Easy language and good example for beginners.Thank you

Posted on 12/19/2009 7:42:32 AM by cju antony

Hi Mamta,

This code is related to Window application you can use it in that.

Thanks

Posted on 12/21/2009 4:16:14 AM by zunnair

"Thread.CurrentThread.Sleep(1000)" should be written as "Thread.Sleep(1000)" because Thread.Sleep() is a shared method

Posted on 2/24/2010 4:08:11 AM by bob

"Thread.CurrentThread.Sleep(1000)" should be written as "Thread.Sleep(1000)" because Thread.Sleep() is a shared method

Posted on 3/17/2010 4:51:52 AM by Rakesh

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Using FtpWebRequest and FtpWebResponse classes using VB.NET in .NET Framework 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net