|
Introduction to Multithreading
Hey, welcome to the first Networking tutorial, Multithreading. This
is a pretty basic tutorial so there's no source code. First of all,
I'd like to thank samsmithnz for submitting this tutorial on
vbProgramming forums (roughly one year ago, I said I'd get to it -
and here it is).
First of all, let's start off with the definition of a thread:
A thread is a process that can run concurrently with other
processes.
Well, when you do Networking, you'll constantly be checking for
messages. To constantly check for messages, you'll need an infinite
loop.
While True = True
'Check For Messages()
End While
That's basically an infinite loop. However, when you run an infinite
loop, your system will crash... being constantly on that particular
loop.
Application.DoEvents
To work around this problem, you could always do:
Application.DoEvents()
While True = True
'Check For Messages()
End While
However, Application.DoEvents pertains only to the application
events, like KeyDown, Paint, MouseUp...etc. So when you do
Application.DoEvents, .NET ends up running the application events on
separate threads. This actually makes the program very slow. Instead
of running the infinate loop on a separate thread, the program would
create a lot more threads to check for application events.
Moreover, if you had another sub (one that you created on your own),
and you did Application.DoEvents, it wouldn't get to it. Like I said
before, it runs only the application events on separate
threads.
Multithreading
Multithreading itself isn't
all that bad. In fact I'll start you off with some code:
'Globals:
Dim x as integer
Dim t as System.Threading.Thread
'Form1_load:
t = New System.Threading.Thread(AddressOf MyInfiniteLoop)
t.Start()
Public Sub MyInfiniteLoop()
While True = True
x += 1
Me.Text = x
End While
End Sub
Run it. It doesn't crash your system now does it? Now close it.
....
.......
Wait a minute, it's not closing!
When you have power, you have to use it wisely. Multithreading is
not a feature found in, for example Visual Basic 6.0. This is an
advanced feature in Visual Basic.NET, and you must do things right.
Just remember, you have power over everything when it comes
to things advanced such as this (although as simple as it may seem).
The reason it's not changing is because the thread is still running!
Add the following code:
'Form1_closing:
t.Abort()
This will stop the thread when the form closes.
NOTE: This took me nearly 30 minutes to figure out, but for some
reason .NET might act all weird to you and decide not to close the
thread even if you do Abort.
It was really weird.. on one project, I had to type t.Abort() about
7 or 8 times for it to actually work. But yet on another project,
with the exact same code, it worked with only one t.Abort().
Then I closed the first project and opened it again, and it worked
with one t.Abort() call. Really strange... Microsoft. Jeez.
That's basically it for this tutorial. It's pretty basic
code. The AddressOf method is covered in the AddHandler tutorial.
It's basically the memory address of that particular sub.
Check out the next
tutorial: Setting up a Local TCP Server!
There is no source
code for this tutorial as it is relatively straightforward. |