Intro to Multi-Threading in VB
Revision 1.0.0
By: AxE (voodooorbital@hotmail.com)

===Intro============================================================

This is my second text, I don't do spell check, and I don't write a lot.
This text is also ment for someone who knows about VB.

Multithreading in VB in some cases can be dangerous, and you might need to
reboot your system. Who cares though, Windows makes you do that anyway. But
still, if you mess up your computer or anything its your own fault. I'm just
providing information.

===Getting started==================================================

In order to multi-thread in VB you need two API calls:

Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, 
ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, 
ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long,
 ByVal dwExitCode As Long) As Long

CreateThread will start the thread, and TerminateThread will terminate it..
suprise suprise. Add this global also:

Dim id As Long

I leave all the control names their defalut names for simplicity.
Put all the code listed in this this secion in your form's general
declarations.

===Setting everything up============================================

Add a textbox that is multi-lined and has vertical scroll bars and two
command buttons to your form. Label one "Start" and one "Stop"

Create a Function, the code should look something like this:

Public Function AddText()
    Do
        Form1.Text1.SelText = "Adding to Text1 - "
    Loop
End Function

This function MUST be in a module

You should know not to run this because if you did you would have to kill
VB and you would loose your work and have to start over again. This is 
when multi-threading becomes your best friend. Of corse you will want to 
use multi-threading for something else, but this is just a example ;)

I know this should be a sub too, but multi-threading only works with
functions (or so I think, I don't want to "waste time" and try it out,
let me know if I'm wrong ;))

===Starting a thread================================================

This is what your Command1 (Start) button should be coded like:

Private Sub Command1_Click()
    id = CreateThread(ByVal 0&, ByVal 0&, AddressOf AddText, ByVal 0&, 0, id)
End Sub

We use 'ByVal 0&' because we don't care about those, and just put a NULL value.

'AddressOf AddText' will tell the api function where the function you want
to multi-thread is located. You can just use the same code and have it go to
the address of what ever functions you want to multi-thread.

'id' sets the thread number, so you can use it to kill the thread when you are
done with it.

===Ending a thread==================================================

This is what your Command2 (Stop) button should be coded like:

Private Sub Command2_Click()
    Call TerminateThread(id, ByVal 0&)
End Sub

On creating a thread, you held its PID in the variable id, so you know
where it is when you want to kill it.

You MUST end a thread before you end your program, otherwise you will have
a loose thread running on your computer... thats not good.

===Whats going on===================================================

You might think this is no big deal, but I dare you to try that function
without multi-threading it first! Your program will freeze, and you wont
be able to do anything. It will just scroll text in the textbox as it runs.
You will have to kill VB to get out of it.

For a experiment, add a DoEvents in the loop, and run it wihout
multi-threading. You will see that you can click on the tile bar, move it 
around as you wish, and close it also. But notice how when you do anything
else the scrolling of the text will stop? You can only do one thing at a
time...

Now, when you multithread the function, fun things happen :) If you scroll
the window around, you will notice that the text is still being added to
the TextBox! The program is now able to do more than one task.

Like I said, it might not seem so great right now, but this is just an 
example... imagine never needing to use DoEvents again, and able to do
as many tasks as your CPU can handle at one time... for most compuers now 
thats a lot of tasks ;)

The problem with multi-tasking is, VB still doesnt like multi-taking, so
you must compile to run your programs with multi-tasking to run correctly.
Most of the time if you run it from VB it will just not go back to the 
programming part of VB (and you need to kil it), or VB will close completly.

===Conclusion=======================================================

You now know how multi-threading works. You will be able to get rid of all 
(almost all) of your 'DoEvents' and your programs will run much faster.

After a few crashes you should get the hang of it :)

I wrote this tutorial for www.Axion-Network.net, but since me information is
free you can distribute it anywhere as long as you leave everything unchanged.
If you feel something needs to be changed or added, e-mail me.

For all my texts go to Http://kickme.to/axe - another good place is
http://gremlin.vectorstar.net/cgi-bin/1.cgi - but because of grem's hate for
VB I decided not to even bother him with this text ;)

Sence this is my second text, and I wrote them both in the same day they
look very much like the same... I don't know if I want it my style or what
at the time.
