Click here to Skip to main content
Click here to Skip to main content

Multithreading with VB.NET - A beginner's choice

, 8 Aug 2006
Rate this:
Please Sign up or sign in to vote.
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

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

 
QuestionDecent Intro Pinmembermajict21-Jun-14 11:21 
QuestionGreat starting point PinmemberCyrus-IRA17-Mar-14 13:26 
GeneralMy vote of 5 PinmemberJOE MIS11-Sep-13 19:18 
QuestionWhich language to use for GCS PinmemberMember 99565733-Apr-13 19:46 
GeneralMy vote of 1 Pinmembersandippatil4-Feb-13 19:07 
QuestionDraw graphics Pinmemberhari ram yadav22-Jan-13 20:30 
GeneralMy vote of 5 Pinmemberwayvirgo6-Jan-13 10:45 
GeneralMy vote of 5 PinmemberOsama Al Shammari21-Dec-12 7:33 
GeneralThanks! Pinmemberitcywel2-Aug-12 17:37 
GeneralMy vote of 5 PinmemberUltronX3-Jul-12 1:40 
GeneralMy vote of 5 Pinmembermanoj kumar choubey5-Mar-12 21:23 
QuestionTAPI PinmemberMember 79538385-Jan-12 6:28 
GeneralMy vote of 5 PinmemberSandro7022-Nov-11 23:40 
QuestionConfused: explain Invoking the main window.. PingroupArchie_dev2-Jul-11 21:32 
Generalgreat example PinmemberMember 116050224-May-11 7:49 
GeneralMy vote of 5 PinmemberMember 466330528-Mar-11 23:25 
GeneralButton4,6 and 8 code errors PinmemberMember 384809418-Mar-11 5:54 
GeneralRe: Button4,6 and 8 code errors Pinmemberpodman@guru25-Nov-11 5:18 
GeneralMy vote of 5 PinmemberNIRMAL UPADHYAY28-Feb-11 22:12 
GeneralMy vote of 5 PinmemberMember 14582303-Jan-11 22:03 
GeneralMy vote of 5 Pinmembersandeep00773417-Nov-10 8:38 
GeneralMy vote of 3 Pinmemberv65j9-Jul-10 18:37 
GeneralMy vote of 1 Pinmembershaoun10005-Dec-09 14:53 
GeneralRe: Multithreading PinmemberRakesh Mehta24-Nov-09 20:11 
Hi there,
 
In simple word delegate is a pointer to a method. While using THREAD you can not simply call a function on a different thread (for example in a form) from within the thread. In this situation we need to create a delegate which will point to the function we need to call, and from within the THREAD you invoke the delegate and then it will call the function pointed by it. Thus you can avoid CROSS THREAD operation in your code. One thing here, remember that you can call a function of one thread from another, but that function call should not change any component within the method called from the thread. That means, if you are calling a function on a form from within the thread and if the function in the form tries to update a text in a textbox, or a label or any other control, then you will receive a CROSS THREAD operation error. At the same time if the function on the FORM does not do any visual element update, then
 
For example you have
1. A form (which is ultimately a class)
2. A class which contains a big function to do some continuous job
 
Now to run a thread, what you do is you create an object of the class and then you create a thread and you mention a function inside the class as a starting point of your thread.
 
Now if you need to call a function on the form from within the function inside the threaded class, you can not call it directly like form1.myFunction() as this may cause an invalid CROSS THREAD operation.
 
What you need to do is you need to create a delegate that will point to myFunction() on the form and then you need to call myFunction() using the delegate (function pointer)
 
to create a delegate you simply have to declare the delegate function with the same signature of the function you want to point.
 
For example.. if myFunction() looks like this
 
private sub myFunction(byval v1 as int32, byval str as string)
 
the the delegate delcaration should be
 
private delegate sub DelMyFunction(byval v1 as int32, byval str as string)
 
Note: The name of the delegate method could be anything.
 
now you need to create an object to the above delegate and point the object to the function on the main form
 
dim x as new DelMyFunction(addressof Form1.myFunction)
 
now x becomes the pointer to the function myFunction and you can use x to call the function.
 
Form1.invoke(x, arguments() as object)
 
our myFunction() takes two argument, but if you see the above line, then there is no way to pass multiple argument. There is only an array of Object. You have to put all your arguments in this Object array. Like
 
redim arg(1) as object
arg(0) = 5 (our first argument in function is an int32)
arg(1) = "myname" (second argument is a string)
 
and you pass the argment as
 
Form1.invoke(x, arg)
 
This will call your function myFunction on your form and will pass the arguments sequentially as you put it in the object array.
 
Hope this helps you to understand how to work with delegate Smile | :)
 
You can refer MSDN for more code snippest http://msdn.microsoft.com/en-us/library/system.delegate.aspx[^]
 
Rakesh Mehta
 
Director
AM-Soft

GeneralRe: multihread using ado.net PinmemberRakesh Mehta16-Nov-09 3:49 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 9 Aug 2006
Article Copyright 2006 by Rakesh Mehta
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid