Jun 23, 2011 01:50 PM
(This post was last modified: Jun 24, 2011 04:26 AM by KingCobraXD.)
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:
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:
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:
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:
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:
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).
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:
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
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 SubExample 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 SubThis 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()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 SubTwo 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 SubNOTE: 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 SubI 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



![[Image: 2u9Smct.png?1]](https://i.imgur.com/2u9Smct.png?1)

![[Image: Yucwo7j.png]](https://i.imgur.com/Yucwo7j.png)