Multithreading with VB.NET - A beginner's choice






3.45/5 (60 votes)
Aug 9, 2006
1 min read

395167

18351
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.
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