Learn,multithread,easily,effectively,with,pow
Quick Search for:  in language:    
Learn,multithread,easily,effectively,with,pow
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
RentACoder Stats

 Code:  lines
 Jobs: 0 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for RentACoder.
Wrapping Scrolling Text
By Paranoid_Androi d on 7/2


Create A Dummy File
By AML on 7/2


Click here to see a screenshot of this code!Captionbar manipulation!
By Peter Hebels on 7/2

(Screen Shot)

A Game Of War
By Co0nest on 7/2


Click here to see a screenshot of this code!KeyGen Example
By Bengie|NET on 7/2

(Screen Shot)

Click here to see a screenshot of this code!OpenBrowser v1.9
By Orlando Jerez on 7/2

(Screen Shot)

SendMessageBySt ring() Example
By Jaime Muscatelli on 7/2


Click here to see a screenshot of this code!FirstSunday
By Jan Paul Penning on 7/2

(Screen Shot)

Click here to see a screenshot of this code!Ikonz v1.0
By Gaurav Creations on 7/2

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

MThreadVB - The Generic Multithreader For VB

Print
Email
 

Submitted on: 5/29/2002 9:54:04 PM
By: Srideep Prasad  
Level: Advanced
User Rating: By 218 Users
Compatibility:

Users have accessed this article 2721 times.
 
(About the author)
 
     Learn how to multithread easily and effectively with this powerful award winning component. With a host of intuitive functions and powerful ActiveX(tm) events ! To find out more, download now !!!

This article has accompanying files

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Multithreading

Multithreading in VB - Where the waters become murky....

Multithreading, as far as VB is concerned, is very difficult to implement. Though VB supports multithreading, it basically supports it only for ActiveX EXEs and to some extent in ActiveX Dlls (The ActiveX Dlls basically allow very primitive multithreading - It just maps instances of itself onto client threads that request its services. If your client is single threaded, the DLL too does not multithread !)

Sometime back I had demonstrated a method of multithreading using ActiveX EXEs. Though it is very stable, the trouble is the code tends to be very cumbersome... So I have designed a new generic multithreader, that, though not as stable as multithreaded ActiveX EXEs, is sufficient for most uses...

A Bit of history....

Soon after I demonstrated how to multithread using ActiveX EXEs, I realized that though the technique was useful, and though most of you appreciated it, it tended to be somewhat cumbersome and then I began to think of ways of creating multiple threads using other methods.... Soon afterwards, I came across an article written by an extremely talented and innovative programmer, Matt Currland (who is unfortunately not a member of PSC), where he demonstrated how to use CreateThread() safely with VB.. But the problem was, the example was very complicated and difficult to understand owing to the many class modules it used and the zigzag nature of execution... After hours or effort, punctuated by crashes, freezes and quite a few reboots, using some inspiration from his article I have created MThreadVB. Though it may not be as "technically right" as his code, I had to sacrifice some of the correctness for ease of use and the generic architecture.... (Which his code sadly did not provide - His was basically a demo program)

Some Technical Stuff ....

How does the multithreader work ?! You might ask - Here's the answer !

Normally, all VB programs are heavily dependent on the runtime DLL for its functioning.In VB 6, within the multithreaded function (Called By the CreateThread() API once the thread has been created) , any calls to the runtime DLL fails (due to some reason perhaps which only the Microsoft VB team might know !) causing your program to crash immediately....
Even an API call is not really compiled in "real" native code in VB and is interpreted by the runtime DLL...
An API call too thus ultimately involves calling the runtime DLL,that causes VB to crash.

Those of you who do not believe this can do a simple test - Open the API viewer, select the mciExecute API defied in the Win32API.txt file... Place it in a project and compile it.... Even though this API is a part of WinMM.Dll, you will find no reference to the WinMM.Dll in case you view the executable in the Depencency Viewer (since the code referencing the DLL is not placed "explicitly" in the executable. When you call the API, the actual call to WinMM.Dll is ultimately made by the runtime DLL only) .... But if you were to open the EXE in MS-DOS editor, you can see the text strings "winmm.dll" and "mciExecute" !

Most standard VB statements and functions such as Set = , For...Next etc also call the runtime DLL and ultimately even these fail... (So much for native 'code compilation !)

If an object could be created within the multithreaded procedure, the VB runtime starts behaving properly...
The trouble is,the standard VB instantiator functions fail within the multithreaded procedures...
Therefore, I have used the ThreadAPI.Tlb type library to bypass the Runtime and directly call the OLE/COM 
APIs (This can be verified using the Dependency Viewer) and create a dummy object inside the multithreaded
procedure (using the CoCreateInstance API). After this has been done, the runtime DLL starts 'behaving properly and it is possible to call all VB functions safely...

Some features and the Do's and Dont's -

1>The multithreader is completely event based, that is the multithreader notifies the client app of any event such as threads terminating, or the a priority change...

2>For creating all a programmer has to to is the call the CreateWin32Thread() Function, and relax ! So no need to fiddle around any more with ActiveX EXEs !

3>To perform File I/O and to show forms from within threads (which were earlier not supported) do the following -

    Sub MThreadProc(DummyArgument as Variant)   'Your multithreaded procedure
        'Some code....
        pThread.ObjectInThreadContext.SomeSubroutine   
    End Sub

    'In the above code SomeSubroutine is a Sub or can be a function defined in the same form or object in which the Sub MThreadProc is defined (pThread is a reference to the DLL)

    Sub SomeSubRoutine       
        'Your file I/O code or code for creating and displaying forms (Form1.Show etc) goes here 
   End Sub

Special Note: In the above statements, the ObjectInThreadContext property returns a reference to the object containing the multithreaded procedure in context to the new thread 

4>Always call the END statement when you want to end your app

I must thank Robin Lobel for reporting the form show bug(it was never noticed by me) also Willian Tarlton who induced be to think until I got a solution, after he reported his extreme need for file i/o within multithreaded procedures

5>As far as posible do not use the MThreadVB component within the IDE - Use it only with compiled EXEs

Please mail me at srideepprasad@digitalme.com if you find bugs or have any suggestions

New ! This article has been updated to include a new class called ThreadLaunchEX that allows multiple thread creation in real time. Though it has not been explained, I must say that it can be used just as the Thread class (used in the Demo) - Only you must identify all threads with a unique threadId parameter. This is not necessary if you are only using the simpler Thread class !

Please vote if you find the code useful... Thank You !


A strange "Dissappearance" and a vote of Thanks !
I must first of all thank all my fellow programmers and developers at PSC for their tremendous response to this code... 

However, recently, this piece of code (along with almost 500 others) was deleted due to site hacking. Therefore, I am resubmitting it.. The good news is that Ian, one of the people behind PSC is sincerely pursuing this matter to bring the hacker to justice...!

(For more information on the hacker attack please visit - (http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=35215&lngWId=1)

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 Winzipto 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.

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

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 8 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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
5/30/2002 2:46:40 AM:Srideep Prasad(AUTHOR)
IMPORTANT: THIS SUBMISSION IS MANY MONTHS OLD! IT WAS DELETED DUE TO SITE HACKING BY A UK BASED HACKER! FORTUNATELY, IAN, PSC's ADMIN HAS RESTORED THE SUBMISSION AND ITS VOTE COUNT! HOWEVER THE VISITOR COUNT WAS NOT RESTORED UNFORTUNATELY! IN OTHER WORDS THERE HAS BEEN NO CHEATING - IN CASE OF DOUBT, PLEASE CONTACT IAN AT IanIppolito@exhedra.com
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/31/2002 4:37:07 AM:Wim Baeyens
Thank you for this excellent code. I don't have a use for it right now but i will surely use it in the future. It's also very good to have an article with it with some background info. Keep up the good work !
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/26/2002 10:42:09 AM:Mark Davis
This is excellent code and a nice packaging. Very simple solution with a nice class interface. Thanks for the submission!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/30/2002 11:25:13 AM:Walter Brebels
THANK YOU, i didn't saved my life :), i didn't knew what all the fuzz wass about Multi-Threading, since everyone told me how you did it but not what you can do with it, thanks a lot, 5 from me
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | RentACoder Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.