ARTICLE

Developing a multi-threaded application using VB.NET

Posted by Mahesh Chand Articles | Visual Basic 2010 August 19, 2002
This article explains about developing a multi-threaded application in VB.NET. Writing multithreaded application in .NET and VB is pretty easy. This tutorial is for beginners who have not coded any multithreaded application in VB yet.
 
Reader Level:

 

Writing multithreaded application in .NET and VB is pretty easy. This tutorial is for beginners who have not coded any multithreaded application in VB yet. Just follow these simple steps.

Define Namespace

In .NET, threading functionality is defined in System.Threading namespace. So you have to define System. Threading namespace before using any thread classes.

Imports System.Threading

Start a Thread

The Thread class of System.threading namespace represents a Thread object. By using this class object, you can create new threads, delete, pause, and resume threads. The Thread class creates a new thread and Thread.Start method starts the thread. 

Private thread As Thread = New Thread(New ThreadStart(WriteData))
thread.Start()

Where WriteData is a function which will be executed by the thread. 

Protected Sub WriteData()
Dim str As
String
For
i As Integer = 0 To
10000
str = "Secondary Thread" & i.ToString()
Console.WriteLine(listView1.ListItems.Count, str, 0,
New String
(){""})
Update()
Next
i
End Sub

Aborting a Thread

Thread class's Abort method is called to abort a thread. Make sure you call IsAlive before Abort.

If thread.IsAlive Then
thread.Abort()
End If

Note: When a call is made to the Abort method to destroy a thread, the common language runtime(CLR) throws a ThreadAbortException. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block. When this exception is raised, the runtime executes all the finally blocks before killing the thread. Since the thread can do an unbounded computation in the finally blocks, you must call the Join method to guarantee that the thread has died. Join is a blocking call that does not return until the thread actually stops executing.

Pausing a Thread

Thread.Sleep method can be used to pause a thread for a fixed period of time.

thread.Sleep()

Setting Thread Priority

Thread class's ThreadPriority property is used to sets thread's priority. The thread priority can have Normal, AboveNormal, BelowNormal, Highest, and Lowest values. These are self-explanatory.

Private thread.Priority = ThreadPriority.Highest

Suspend a Thread

The Suspend method of the Thread class suspends a thread. The thread is suspended until Resume method is called.

If thread.ThreadState = ThreadState.Running Then
thread.Suspend()
End If

Resume a suspended Thread

The Resume method is called to resume a suspended thread. If thread is not suspended, there will be no effect of Resume method.

If thread.ThreadState = ThreadState.Suspended Then
thread.Resume()
End If

share this article :
post comment
 

Its about the code IN VISUAL studio 2010 for a tray application. i want clicking on it should open the cd drive and application should wait for me to again click and then cd drive should close.

Posted by subu Feb 17, 2012

i want to rec data onto the socket which will be pass to new thread, i m new to socket programming

Posted by Lakul Jotaniya Mar 26, 2008

i'm using thread.abort() to terminate thread in my application in a button click event .I'm using threading.threadabortexception in catch block to catch the error. but still i'm getting the error as "ObjectDisposedException was unhandled " .

Posted by srinivas reddy Mar 20, 2008
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor