Click here to Skip to main content
15,888,454 members
Articles / Programming Languages / Visual Basic
Article

Multithreading with VB.NET - A beginner's choice

Rate me:
Please Sign up or sign in to vote.
3.45/5 (60 votes)
8 Aug 20061 min read 382.5K   18.3K   104   92
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.

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

VB
'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


Written By
Web Developer
India India
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.

Comments and Discussions

 
GeneralRe: A doubt in the concept of passing parameters in multithreading Pin
Rakesh Mehta18-Dec-08 18:46
Rakesh Mehta18-Dec-08 18:46 
GeneralRe: A doubt in the concept of passing parameters in multithreading Pin
harimv200226-Sep-12 20:56
harimv200226-Sep-12 20:56 
QuestionGoto?!? Pin
eyeball_200314-Oct-08 1:32
eyeball_200314-Oct-08 1:32 
GeneralSuperb :) Pin
rohits_rods24-Sep-08 15:25
rohits_rods24-Sep-08 15:25 
QuestionHow to pass parameter to passive thread? Pin
bondan_intikom19-Aug-08 20:26
bondan_intikom19-Aug-08 20:26 
AnswerRe: How to pass parameter to passive thread? Pin
Rakesh Mehta19-Aug-08 20:48
Rakesh Mehta19-Aug-08 20:48 
QuestionRe: How to pass parameter to passive thread? Pin
bondan_intikom19-Aug-08 21:18
bondan_intikom19-Aug-08 21:18 
AnswerRe: How to pass parameter to passive thread? Pin
Rakesh Mehta19-Aug-08 21:31
Rakesh Mehta19-Aug-08 21:31 
Hi Bondan,

Now I understand what you are trying to do.. You are having a confusion over here.

Alaways keep one thing in your mind. i.e

A thread is a function that run continously. If we dont need a continuous loop (which can hang our program), then why do we need a thread?

Is not it? If there is no loop in the threaded function, then as soon as the execution of the function completes, the thread will die.

If I have understood properly, then what you are trying to do is.. If anything happnes on your form you want to notify someone to do some other task.

If this is the case, then you will have to use windows sendmessage, postmessage api to communicate between two processes. Notice here, its not thread, its two different process.

For example, you may have 2 projects, 1 is with GUI and another without GUI, can run as service or with a hidden form. The GUI less project sleeps, while it does not have to do anything. So its an IDLE process, and does not use CPU. Now when something happens on your main application you can send a message to your GUI less process to do something..

Instead of using process, you can also acheive the same using a hidden form..

For example, whatever you are trying to do within the threaded function, you write all the codes inside that hidden form.. and you can send a message to that hidden form, from your main form using that hidden windows handle (FORM.handle) and sendmessage or postmessage API


Hope this helps you a bit Smile | :)

Rakesh Mehta

Director
AM-Soft

QuestionHow to make multithreading class? Pin
bondan_intikom3-Aug-08 19:00
bondan_intikom3-Aug-08 19:00 
AnswerRe: How to make multithreading class? Pin
Rakesh Mehta3-Aug-08 19:24
Rakesh Mehta3-Aug-08 19:24 
QuestionHow to pass parameter into running thread? Pin
Bondan Wisnuwardhana21-Jul-08 23:25
Bondan Wisnuwardhana21-Jul-08 23:25 
AnswerRe: How to pass parameter into running thread? Pin
Rakesh Mehta22-Jul-08 0:04
Rakesh Mehta22-Jul-08 0:04 
GeneralRe: How to pass parameter into running thread? Pin
Bondan Wisnuwardhana22-Jul-08 18:11
Bondan Wisnuwardhana22-Jul-08 18:11 
QuestionHow to make multithreading library? Pin
Bondan Wisnuwardhana29-Jun-08 19:21
Bondan Wisnuwardhana29-Jun-08 19:21 
AnswerRe: How to make multithreading library? Pin
Rakesh Mehta29-Jun-08 20:42
Rakesh Mehta29-Jun-08 20:42 
GeneralRe: How to make multithreading library? Pin
Bondan Wisnuwardhana2-Jul-08 20:16
Bondan Wisnuwardhana2-Jul-08 20:16 
Generalbugs in If Not m_ThreadList(1) Is Nothing Then If CType(m_ThreadList(1), Thread).IsAlive Then Pin
raysky8-Jun-08 16:52
raysky8-Jun-08 16:52 
QuestionGOTO? Pin
Metaphore7-Oct-07 19:55
Metaphore7-Oct-07 19:55 
GeneralMultithreading in web services Pin
usha gupta7-Aug-07 20:50
usha gupta7-Aug-07 20:50 
GeneralRe: Multithreading in web services Pin
sikunj patel7-Jan-09 0:31
sikunj patel7-Jan-09 0:31 
QuestionHow to use a main thread to start 4 sub threads Pin
Ramsky30-May-07 11:56
Ramsky30-May-07 11:56 
AnswerRe: How to use a main thread to start 4 sub threads Pin
Skylinc9-Nov-09 23:37
Skylinc9-Nov-09 23:37 
QuestionA request from "Mr. Raj" Pin
Sean Ewington2-Apr-07 9:06
staffSean Ewington2-Apr-07 9:06 
QuestionWhen do you use threading? Pin
gideon etan6-Mar-07 8:37
gideon etan6-Mar-07 8:37 
Generalproblem in Multi threading Pin
NVMehta26-Feb-07 3:10
NVMehta26-Feb-07 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.