Click to See Complete Forum and Search --> : How can i write multi-thread program in VB?


Chan Shi wing
March 26th, 1999, 09:05 PM
Hi! Everybody!

I want to know how to write create another thread like VC++ function

AfxBeginThread. Could i achieve similiar function in VB? Besides, i try

to create another thread from a DLL generated by Visual C++ but something

is string happening. Is it right to do this?

I hope anyone could help me!

Thank you very much!

Crazy D
March 27th, 1999, 11:13 AM
Hi


As far as I know, the create thread functions doesn't work in VB. (And VB doesn't have it's own functions for it).

What I heard, if you create an ActiveX exe you create an out-of-process ActiveX component... which has it's own thread (while an ActiveX runs in the threat of the app).

I haven't really checked this since I dind't need it, but check the help about AcitveX exe's and see what they say about it.


Hope this helps


Crazy D

Ravi Kiran
March 28th, 1999, 10:36 PM
Hi,


I tried and FAILED using 'Createthread' API with 'AddressOf' operator in VB.

I dont know it failed because 'SecuirityDescriptors' were wrong or it did not

find the proper callback function. Though createthread accepts NULL for

Sec.Desc. in VC, we cannot pass a NULL because no 'NULL' defined and with anyother value this fn fials.


My solution was to write a dll and do things in there.

Somethings are a snap in VC, and somethings are a snap in VB (try painting in Dialogs using VC:-) )!


Using Active-X exes is the solution to run on multiple threads, but your server needs to be registered properly => Setup program needs to be good. During development, things will go smoothly because you can use 2 instances of VB and put your server in run-mode in one and start your client program in other.

Once both exes are ready, and you want to try on a user machine.., Best of Luck!. Active X serves created VC++ has auto registry update when run in stand alone mode i.e w/o '/Automation' flag, and usecan use this option to register your server. I think, I think.., there is no such facility in VB.


Hence i ended up using a simple (windows) Dll and threading in VB..


Ravi

Chris Eastwood
March 30th, 1999, 03:20 AM
Hi


Dan Appleman wrote a (ver large) article about how to implement multi-threading in VB (not using CreateThread) - have a look around at :


http://www.desaware.com


Regards


Chris Eastwood


CodeGuru - the website for developers

http://www.codeguru.com/vb

Ravi Kiran
April 1st, 1999, 08:39 PM
Hi Chris,

Thanks for the link. In there he explains a lot of good stuff..
VBs new form engine that handles Forms on sep. threads was a revelation!!, worth some thought.
---

The gotcha was in Decalre statement, for CreateThread.

As per Dan's code, i too modified the sercurity desc. declaration to 'Byval SecDesc as long', insted of 'ByRef lpSecDesc as SECURITY_DESC..' as given in WinApi.txt. and always pass 0.
As for (LPVOID) lpParams , Dan makes it 'ByVal & Long' and i left it 'Byval & Any' and i managed to pass a structure UDT across, with Varptr, which stores all the data that the thread fn need to access.
and it works!!, err., suffices my requirement. I dont intend to handle GUIs on threads anyway,just plain, simple, long processing tasks, that need to be interupted; that to allowed only at particular intervals in the loop...

Ravi

ps : I should add Appleman's comment as ps:
CreateThread is not meant to be used like this. If it works now, no gaurantee for next version of VB... or something to this effect.

2. May be they should do something about this kind of playing around with Decalre statements?
"Hardcore Visual Basic" by ??? also has something on this, right?