Multithreading - Understanding the pros and cons
One of the greatest problems of the earlier Win16 environment was that an application could do only one thing at one time (that is "single thread"). However with the advent of Windows NT 3.5x, this changed. In 1995, with the release of Windows 95 this ultra powerful technique came to be used in the common PC.
So what is the use of Multithreading ? - Consider Microsoft Word 97 or higher. It checks spelling while you type ! It does it by multithreading - i.e running two "threads" (in layman's language a "thread" is nothing but a piece of code, a sub or function running simultaneously with the main program). In VB 5, a new function AddressOf was introduced that enabled VB programmers to get the address of any public function in a standard module. This enabled developers to use the CreateThread API to create raw Win32 threads. Though this was effective in VB 5.0, with VB 6.0 it crashes miserably !
Even at planet-source-code.com, I came across a multithreading demo using the CreateThread API. Though THE PROGRAM works with VB 6, it is VERY unstable. Also For ... Next loops, Msgbox..., Open .. etc statements do NOT work in the multithreaded procedures!
Does this mean we cannot multithread safely !? Does it mean that we have to worry about
"exception errors" and GPF's popping up any time ?
The Answer is a BIG NO !
Multithreading is VERY easy once you master the concepts... So just have a look at the sample code.. You will understand just how easy it is to perform true and safe multithreading in VB !
If you gained any information, or if this article is useful to you, a vote of yours will be appreciated. If you found it useless.... just DELETE it !
Multithreading In VB 6 - The Safe Way
The trick to effective and safe multithreading in VB is to use the ActiveX EXE project type (set to standalone EXE). The trick here is to create a new object on a new thread by callin the CreateObject() function and to create the Form that you want to be multithreaded from within this object. As a reult the form is created on a new thread, both of them can run almost independently of the other ! The only problem is managing the code re-enterancy - VB calls the Sub main() procedure every time a new object is created - we must find whether the main window is shown or not - if not we must initialize it. This method is actually very easy ! Just check out the sample code and I am sure that you will be Multithreading right
What's more - this code now even demonstrates how to communicate between
And if you found this code useful - be sure to vote for me ! After all, coding is a tough job, and so is writing a tutorial
IMPORTANT: You can now download a new generic multithreader component at
the following link http://planet-source-code.com/vb/default.asp?lngCId=26900&lngWId=1
. This component allows you to multithread any sub or function in a standard
EXE. No ActiveX EXEs needed (PS:Thanks for all your votes And I am happy to know
that my articles are of some use to you !)