5,427,813 members and growing! (14,917 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate

Multithreading with VB.NET - A beginner's choice

By Rakesh Mehta

Multithreading is a technique by which you can increase the efficiency of your program by 100%. Everybody wants to apply multi-threading in their own applications. But for beginners, it's a bit difficult to apply multi-threading properly. But it's quite easy with VB.NET.
VBWindows, .NET, .NET 1.1, .NET 2.0, Win2K, WinXPVS.NET2003, VS2005, Visual Studio, Dev

Posted: 9 Aug 2006
Updated: 9 Aug 2006
Views: 32,289
Bookmarked: 43 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
22 votes for this Article.
Popularity: 3.55 Rating: 2.65 out of 5
7 votes, 31.8%
1
1 vote, 4.5%
2
1 vote, 4.5%
3
3 votes, 13.6%
4
10 votes, 45.5%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Sample Image - mutithreading_for_beginer.gif

Introduction

Hi friends,

This is my first article on Code-Project on the topic Multithreading in VB.NET.

Multithreading is a technique, by which you can increase efficiency of your program by 100%. Every body wants to apply multi threading in their own application. But for beginner it's a bit difficult to apply multi threading properly. I had the same problem, when I was going to use it in my application. That's why I am writing this article, that beginner can easily apply multi threading in their application. I am trying to make it as simple as possible.

I am not writing this article against some ones request. Rather than, I am writing this article, as I personally faced a big problem on this topic. It was so hard to find out a good solution or help on "How to apply threading on application".

After so much study, search on google and on other forums at last I was able to apply multi threading in my application.

Multi Threading is not a critical or complex thing to do. But many people don't now how to apply it. They can get sample code, but can't implement it on their own project. Or they face trouble, when they try to apply the code in their application.

I am not writing much code here. Because, if you download the source code, provided with this article, you will find comment through out each line of coding. So I am not repeating them again here

The heart of this project is the following line of coding

'The main mother class in .NET Framework to work with threading. 

Imports System.Threading  

'Creating Thread 
Dim objThreadClass As New clsThread(2, Me)
Dim objNewThread As New Thread(AddressOf objThreadClass.StartThread)
objNewThread.IsBackground = True
objNewThread.Start()
m_ThreadList.Item(1) = objNewThread 

'Receiving Message from thread
Public Sub ReceiveThreadMessage(ByVal ThreadIndex As Integer, _
                                ByVal Counter As Integer) 
    'do your work here depending on the return variables from the thread. 
End Sub 


'Sending Message from thread to the main application.
'First create a delegate (For details plz see the comments inside the codes) 

Private Delegate Sub NotifyMainWindow(ByVal ThreadIndex As Integer, _
                                      ByVal Counter As Integer)
'We need an object of this deletegate
Private m_NotifyMainWindow As NotifyMainWindow  

'Assign the pointer to the method of the main window to the delegate
m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage 

'Create Argument List

ReDim m_Args(1)
m_Args(0) = m_ThreadIndex
m_Args(1) = m_Counter

'Call the notificaiton method on the main window by the delegate
m_MainWindow.Invoke(m_NotifyMainWindow, m_Args)

If this helps you, I'll be so happy

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Rakesh Mehta


Hi,
This is Rakesh Mehta from India.
I am very very fond of programming (in any field).
I like to work on new technologies. Sometime, I face troubles, while looking for a new thing.
I am working as a Project Manager in Silicon Valley Infomedia Pvt Ltd. (India).
I'ld like to share all my new findings and solutions of problems with code project.
Occupation: Web Developer
Location: India India

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 33 (Total in Forum: 33) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionHow to pass parameter to passive thread?memberbondan_intikom21:26 19 Aug '08  
AnswerRe: How to pass parameter to passive thread?memberRakesh Mehta21:48 19 Aug '08  
QuestionRe: How to pass parameter to passive thread?memberbondan_intikom22:18 19 Aug '08  
AnswerRe: How to pass parameter to passive thread?memberRakesh Mehta22:31 19 Aug '08  
GeneralHow to make multithreading class?memberbondan_intikom20:00 3 Aug '08  
GeneralRe: How to make multithreading class?memberRakesh Mehta20:24 3 Aug '08  
QuestionHow to pass parameter into running thread?memberBondan Wisnuwardhana0:25 22 Jul '08  
AnswerRe: How to pass parameter into running thread?memberRakesh Mehta1:04 22 Jul '08  
GeneralRe: How to pass parameter into running thread?memberBondan Wisnuwardhana19:11 22 Jul '08  
GeneralHow to make multithreading library?memberBondan Wisnuwardhana20:21 29 Jun '08  
GeneralRe: How to make multithreading library?memberRakesh Mehta21:42 29 Jun '08  
GeneralRe: How to make multithreading library?memberBondan Wisnuwardhana21:16 2 Jul '08  
Generalbugs in If Not m_ThreadList(1) Is Nothing Then If CType(m_ThreadList(1), Thread).IsAlive Thenmemberraysky17:52 8 Jun '08  
GeneralGOTO?memberMetaphore20:55 7 Oct '07  
GeneralMultithreading in web servicesmemberusha gupta21:50 7 Aug '07  
GeneralHow to use a main thread to start 4 sub threadsmemberRamsky12:56 30 May '07  
QuestionA request from "Mr. Raj"staffSean Ewington10:06 2 Apr '07  
QuestionWhen do you use threading?membergideon etan9:37 6 Mar '07  
Generalproblem in Multi threadingmembernmehta0054:10 26 Feb '07  
GeneralRe: problem in Multi threadingmemberRakesh Mehta6:19 26 Feb '07  
Generalthanksmemberwmhp113:44 11 Jan '07  
GeneralThread Index Questionmembereatwork13:14 9 Nov '06  
GeneralRe: Thread Index Questionmembereatwork13:22 9 Nov '06  
GeneralRe: Counter variable problemmemberRakesh Mehta2:17 10 Nov '06  
GeneralRe: Counter variable problemmembereatwork11:40 14 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Aug 2006
Editor:
Copyright 2006 by Rakesh Mehta
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project