Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Multithreading in VB.NET
Multithreading in VB.NET
by John Spano | Published  07/16/2005 | .NET Framework | Rating:
John Spano

John Spano cofounder and CTO of NeoTekSystems, a Greenville, South Carolina technology consulting company. NeoTekSystems offers IT consulting, custom programming, web design and web hosting. We specialize in Microsoft .Net enterprise development and business design.

I have six years of experience in software architecture. My primary focus is on Microsoft technologies, and I have been involved in .NET since beta 1. I currently hold a MCSD certification, 2 MCTS's (Windows, Web) a MCPD in Distributed, 2 MCITP's, a Microsoft MVP, and have won the Helper of the Month contest for July 2002 in the devCity.NET forums.

Corporate URL: www.NeoTekSystems.com
Primary email: JSpano@NeoTekSystems.com
Alternate email: Jspano@devcity.net.

 

View all articles by John Spano...
Introduction

Introduction

    Multithreading, a very powerful technique, is essential for modern software development.  Software users expect to work with a very responsive program that they don’t have to wait on, which is a very reasonable demand with the processor speeds that are currently available.  Enter multithreading.  Multithreading is the concept of having several different paths of code running at the same time.

     When you introduce multithreading in your applications, you immediately make programming more complicated and add design time.  You must know exactly what your application and all its threads are doing at all times.  You have to account for deadlocks, race conditions and corrupting variable values.  In this article we will examine the different methods in Visual Basic.Net to accomplish thread synchronization.  We will learn what deadlocks and race conditions are and how to avoid these common problems with multithreading.

System Requirements

    I will assume that you already have knowledge of basic threading in Visual Basic.Net.  You should know how to create threads and know how to do basic threading operations like Join and Sleep.  A copy of Visual Studio.Net is required to run the code samples and see the output. The code was written with Visual Studio.Net using version 1.0 of the .Net Framework with service pack 2.

Case Study Structure

    This case study has three main parts.  Multithreading requires a technique called synchronization to eliminate the problems described above so we will first take a brief look at what synchronization is.  Then an in-depth look at all methods available in Visual Basic.Net for synchronization will be presented where you will learn how to correctly synchronize a multithreaded application.  After this, a look at Window’s Form synchronization and threading apartment styles will show the differences a programmer must handle between standard synchronization and visual GUI synchronization.

Comments    Submit Comment

Comment #1  (Posted by Steven Leadbeater on 08/02/2005)
Rating
Oh My God. this is absolutely brilliant, clear concise and everything works with a minimal amount of tweaking by FAR the best post on threading i have read including anything on MSDN. only wish i could give it a higher rating, nice1
 
Comment #2  (Posted by Ran Oren on 08/14/2005)
Rating
Thanks a lot!!! So clear and simple!
 
Comment #3  (Posted by an unknown user on 08/24/2005)
Rating
Gave me exactly the information I was looking for in a clear way. Thanks
 
Comment #4  (Posted by an unknown user on 08/24/2005)
Rating
None of the books or articles i've read throughout my career has made me understand multi-threading, more than this article.
 
Comment #5  (Posted by an unknown user on 08/24/2005)
Rating
None of the books or articles i've read throughout my career has made me understand multi-threading, more than this article.
 
Comment #6  (Posted by Sulakshana on 08/25/2005)
Rating
Too good,wish I had found this earlier.
 
Comment #7  (Posted by Anonymous on 09/03/2005)
Rating
I agree wholeheartedly with the other comments. This is the only lucid, accessible article I've ever read on the subject. Mr. Spano ought to consider writing a book on the subject--he obviously knows his stuff and how to explain it for us mere mortals.
 
Comment #8  (Posted by an unknown user on 09/07/2005)
Rating
It covered all the salient points.
 
Comment #9  (Posted by an unknown user on 09/21/2005)
Rating
only one word: Excellent!
 
Comment #10  (Posted by Lucas on 10/06/2005)
Rating
Excellent, perfect, great, but the code snippets could be formatted better.
 
Comment #11  (Posted by an unknown user on 12/01/2005)
Rating
good wana more like this
 
Comment #12  (Posted by Sandy Righolme on 12/20/2005)
Rating
Excellent article. Thanks.
 
Comment #13  (Posted by GaZ on 03/30/2006)
Rating
Absolutely spot on article. Very very helpful thanks!
 
Comment #14  (Posted by Marko Hocevar on 04/03/2006)
Rating
I found the article and was very excited to read it...
I also used a piece of code from here in one of my apps but recently discovered that is not working properly. Because threads never share local dynamic variables example in second Public Sub Foo() where piece of code is locked with local variable (=new object()) is not working at all!!

best regards
Mare
 
Comment #15  (Posted by Zoila on 05/16/2006)
Rating
Great Article, so clear and concise. A monkey can understand it... well maybe not a monkey, but I understood it.

Thanks ...
 
Comment #16  (Posted by an unknown user on 06/25/2006)
Rating
Very well written article.
 
Comment #17  (Posted by matroskin on 07/27/2006)
Rating
The example with invoke method, about synchronization of windows forms, does not work, exception in run time
 
Comment #18  (Posted by rimanning on 08/08/2006)
Rating
The other reviewers got it right - this article explains multithreading with great clarity. Good job!
 
Comment #19  (Posted by an unknown user on 10/06/2006)
Rating
This is a Good article - introduces the subject well - flies at 10,000 feet and I enjoyed as a manager but may not be that useful, as a developer except providing a useful background.
 
Comment #20  (Posted by Assaf on 10/16/2006)
Rating
Excellent!
 
Comment #21  (Posted by an unknown user on 10/19/2006)
Rating
Good article
 
Comment #22  (Posted by Mohammad Hefny on 11/12/2006)
Rating
Very great topic...too advanced yet too clear.
I really learned important issues by reading it.
Thank you
 
Comment #23  (Posted by an unknown user on 02/01/2007)
Rating
it's interesting but is's nice to have source code available.
 
Comment #24  (Posted by an unknown user on 06/30/2007)
Rating
Very Cool Article if you have sample code then please send me at sumit.professional@gmail.com or kumar_chams@yahoo.com
 
Comment #25  (Posted by an unknown user on 08/21/2007)
Rating
The examples in your tutorial has some problems. The following Sub is not threadsafe, not commenting on the fact that the method uses a lock to write a local variable. The actual assignment is not threadsafe since each thread calling the method will create a new objLock instance and lock on that, meaning that each thread will in fact be able to get the lock, and do the assignment.

Public Sub Foo()

Dim iData as Integer
Dim objLock as Object = New Object()
SyncLock objLock
iData = 3
End SyncLock
End Sub

Not a very good example. I would think the same problem counts for the monitor examples also.
 
Comment #26  (Posted by an unknown user on 08/21/2007)
Rating
Better than any other article I have seen.
 
Comment #27  (Posted by mm on 10/31/2007)
Rating
This is an excellent article, thanks. To those who complained about the lock being acquired on a local variable - well, duh. Not like it's rocket science to figure that out....
 
Comment #28  (Posted by an unknown user on 12/13/2007)
Rating
I both agree and disagree with Comment #25.

I agree with #25 in that based on some computer generated code in one of my projects the line "Private Shared objLock As New Object" placed in the same class as the Foo() subroutine would resolve the issues that #25 has pointed out.

I disagree with #25's "Not a very good example" statement because it is both an excellent article with excellent examples. If one mistake is all it takes to make something "Not very good" then I submit to all who read this that little to nothing any has every done has ever been "very good".

 
Comment #29  (Posted by an unknown user on 12/14/2007)
Rating
Hey! You can pass values to DelegateWork with the following code:

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim Thread1 As Thread
Thread1 = New Thread(AddressOf Thread1Work)
Thread1.Start()
End Sub

Private Delegate Sub DelAddItem(ByVal StringHeader As String)
Private Sub Thread1Work()
txtList.Invoke(New DelAddItem(AddressOf DelegateWork), New Object() {"A New Line: "})
Debug.Print("Thread 1 Done")
End Sub
Private Sub DelegateWork(ByVal StringHeader As String)
Dim i As Integer
For i = 0 To 100
txtList.Text = txtList.Text & StringHeader & i.ToString() & vbCrLf
Next 'i
Debug.Print("Delegate Done")
End Sub
 
Comment #30  (Posted by an unknown user on 01/14/2008)
Rating
I have a quuestion, is there a way to implement somthing like a network mutex, where the resources lie on computer 1, and other computers sync to access the resources, please mail me matijasch@yahoo.com, the article is great only this topic is missing, thanks a lot, for helping us
 
Comment #31  (Posted by an unknown user on 01/22/2008)
Rating
brilliant... print and keep, then re-read, re-read, re-read.


 
Comment #32  (Posted by an unknown user on 02/17/2008)
Rating
Great article. Tons of good information. Covered everything that I needed. Thanks, keep writing.
 
Comment #33  (Posted by an unknown user on 02/28/2008)
Rating
Rating says it all
 
Sponsored Links