article

Multithreading with multi-instancing and inter-thread communication support

Email
Submitted on: 1/18/2015 3:12:00 AM
By: Srideep Prasad (from psc cd)  
Level: Advanced
User Rating: By 192 Users
Compatibility: VB 5.0, VB 6.0
Views: 4873
 
     This article teaches how to multithread safely and effectively using pure VB. No need to use C/C++ or the infamous CreateThread API notorious for its instability with VB 6. Simple and effective code complete with a HTML article documenting the various aspects of Multithreading are included to help you get started right away ! What's more, multiple-app instances are also now supported ! So say goodbye to Timers, crashes, threading API's and "freezing" forms. And if you find this code useful, remember that your vote will be GREATLY APPRECIATED ! (NOTE: When you create your own multithreaded app, set the threading model to "Thread per object") NO EXPERIENCE OF C/C++ IS REQUIRED . SO NO NEED TO GO RUNNING ABOUT FOR A VC++ COMPILER. ENTIRELY BASED ON MICROSOFT'S ACTIVEX TECHNOLOGY FAMOUS FOR ITS SCALABILITY AND STABILITY. AND FOR THE SCEPTICS, THIS DEMO CAN CREATE 102 THREADS FOR A MEAGRE RAM MEMORY REQUIREMENT OF 2.5MB ALTOGETHER (A STANDARD BLANK FORM EXE TAKES AROUNG 2.0 MB OF MEMORY IN WIN98 FE) IF YOU FIND THIS ARTICLE USEFUL PLEASE VOTE FOR ME ! [REQUIRES VB 5(Sp2) / VB 6)

This article has accompanying files

 
				

Multithreading


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 away

What's more - this code now even demonstrates how to communicate between threads !

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 !)

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.