Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[VB.NET] Multi-Threading [TUT]

What is a multi-threaded application?
A multi-threaded application is an application that contains more than one thread.

Why are multi-threaded applications better than single-threaded applications?
A multi-threaded application has the ability to do many simultaneous things at once.

Example of a single-threaded application:
Code
Sub Main()
    MsgBox("1")
    MsgBox("2")
End Sub
Basically, MsgBox("2") has to wait for the user to close/click OK on MsgBox("1") before it will show itself. It's like a step by step process and you can only do one thing at a time.

Example of a multi-threaded application:
Code
Sub Main()
    Dim t1 As New Thread(AddressOf one)
    t1.Start()
    Dim t2 As New Thread(AddressOf two)
    t2.Start()
End Sub
Sub one
    MsgBox("1")
End Sub
Sub two
    MsgBox("2")
End Sub

This code will create t1 before t2. However, MsgBox("2") no longer needs to wait for the user to close/press OK on MsgBox("1") before it will show. Both these messageboxes will show around the same time.

In order to create a new thread, you would be required to something like this:
Code
Dim t As New Thread(AddressOf NameOfSub)
t.Start()
It will run whatever is in the sub that you have specified when constructing the thread.

Is that all of threading?

No. Threading is a very useful tool but it is also a very confusing tool. Unfortunately, you are not allowed to edit GUI properties that aren't created in the same thread. Take a look at the code below:

Code
Sub Main()
    Dim t1 As New Thread(AddressOf one)
    t1.Start()
    Dim t2 As New Thread(AddressOf two)
    t2.Start()
End Sub
Sub one
    Dim Form1 As New Form()
End Sub
Sub two
    Dim Form2 As New Form()
End Sub

Two threads are created and both of them create new forms. However, t1 will not be allowed to edit Form2 and t2 will not allowed to edit Form1.

NOTE: When an application runs, it creates its own main thread. The above code creates two threads but it actually has three threads running.

How do I manage to edit forms that aren't created in the same thread?
We use the invoke method along with delegates. Take a look at the code below:

Code
Dim Form1, Form2 As Form()
Sub Main()
    Dim t1 As New Thread(AddressOf one)
    t1.Start()
    Dim t2 As New Thread(AddressOf two)
    t2.Start()
End Sub
Sub one
    Form1 = New Form()
    Invoke(New ChangeTextDelegate(AddressOf ChangeText), Form2, "Form2TextHere")
End Sub
Sub two
    Form2 = New Form()
End Sub
Delegate Sub ChangeTextDelegate(ByVal Frm As Form, ByVal txt As String)
Sub ChangeText(ByVal Frm As Form, ByVal txt As String)
    Frm.Text = txt
End Sub
In the above code, Form2 was created on the second thread and was edited in the first thread. The Invoke method executes the specified Delegate.

NOTE: The Delegate and the Sub that is going to be executed MUST have the EXACT same parameters.

After invoking the delegate, we need to specify which sub is doing the editing by using AddressOf Operator (Code rewritten below).
Code
Invoke(New ChangeTextDelegate(AddressOf ChangeText), Form2, "Form2TextHere")

Last, but not least, directly after the specified sub would be the parameter you want to pass into the sub.

NOTE: You can use MethodInvoker as a delegate without any parameters.

Example Code:
Code
Dim Form1, Form2 As Form()
Sub Main()
    Dim t1 As New Thread(AddressOf one)
    t1.Start()
    Dim t2 As New Thread(AddressOf two)
    t2.Start()
End Sub
Sub one
    Form1 = New Form()
    Invoke(New MethodInvoker(AddressOf ChangeText))
End Sub
Sub two
    Form2 = New Form()
End Sub
Sub ChangeText()
    Form2.Text = "Form2TextHere"
End Sub

I hope you have learned a thing or two about threading. =)

Please leave a comment! It took me half an hour to write this out. It only takes you ten seconds to say THANKS.


© KingCobraXD
Looks good so far. If you wanted to expand it you could talk about ThreadPools, the overhead associated with the creation and destruction of threads, ways of monitoring threads, etc. Just a suggestion, great thus far though. :)
Code
Sub Main()
    For i = 1 To 666
    Dim t1 As New Thread(AddressOf one)
    t1.Background = True
    t1.Start()
    Next
End Sub

Sub one()
    MsgBox("Spam ?")
End Sub
^
Would this code spam message boxes or am I not understanding ?
[Image: 2u9Smct.png?1]
(Jun 23, 2011 01:57 PM)Marvincmarvin. Wrote:  
Code
Sub Main()
    For i = 1 To 666
    Dim t1 As New Thread(AddressOf one)
    t1.Background = True
    t1.Start()
    Next
End Sub

Sub one()
    MsgBox("Spam ?")
End Sub
^
Would this code spam message boxes or am I not understanding ?

Yes. It would. But it would take a lot of memory becuase it's creating so many threads. Try this code:

Code
Sub Main()
    Dim t1 As New Thread(AddressOf one)
    t1.Background = True
    t1.Start()
End Sub

Sub one()
    For i = 1 To 666
  MsgBox("Spam ?")
    Next
End Sub


(Jun 23, 2011 01:57 PM)theonetrueace Wrote:  Looks good so far. If you wanted to expand it you could talk about ThreadPools, the overhead associated with the creation and destruction of threads, ways of monitoring threads, etc. Just a suggestion, great thus far though. :)

Thanks. I will do that some other time. lol.
Good explanation on Multi-Threading. Good job.
If your spamming message boxes you mind as well spam threads ;P
~Thanks for the tutorial btw
[Image: 2u9Smct.png?1]
Why are you using "AddressOf" instead of "sub()"????
Wrong programming technique....
'How do I manage to edit forms that aren't created in the same thread?'
its called crossThread. and its mean that thread want change thing in second thread.
nice sample ... thread B want to change propertie in Thread A [main form -> some label]
so i have to use. this.invoke Or base.invoke
to run that operation in the main thread.
so now A will change the label.
(Jun 23, 2011 03:49 PM)shadow1992 Wrote:  Why are you using "AddressOf" instead of "sub()"????
Wrong programming technique....

Do some research. It's AddressOf....
lol,u posted this what i need on my bday wow :)
thanks !
p.s i hope u release soon ShadowTech Rat
(Jun 23, 2011 04:15 PM)W33DY Wrote:  lol,u posted this what i need on my bday wow :)
thanks !
p.s i hope u release soon ShadowTech Rat

Lol. No prob. Happy BDay!
Thanks for this. Actually I have looked into multi-threading before, but it didn't make perfect sense at the time. But this does. Thank you a lot for this thread.
Fck, 'been looking for this for days.
Good Job mate, Appreciate it

Thanks!
PHP Code

<?php 
Sub Main
()
    
Dim t1 As New Thread(AddressOf one)
    
t1.Start()
    
Dim t2 As New Thread(AddressOf one)
    
t2.Start()
End Sub
Sub one
    MsgBox
("1")
End Sub
Sub two
    MsgBox
("2")
End Sub 
um, doesnt this code just create 2 MsgBox("1")'s instead one of each
[Image: Yucwo7j.png]



Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by mrKOMay 29, 2020 09:50 AM 4 232 May 29, 2020 09:50 AM
Last Post: CD_ROM
[Python] by -DarkByDesign-May 15, 2020 02:45 AM 7 180 May 15, 2020 02:45 AM
Last Post: Athr
[Java] by -DarkByDesign-May 14, 2020 12:13 PM 0 129 May 14, 2020 12:13 PM
Last Post: -DarkByDesign-