can anybody give me an example which uses CreateThread to make a MultiThread VB Project?Thank you very much.
Printable View
can anybody give me an example which uses CreateThread to make a MultiThread VB Project?Thank you very much.
As far as I know, there is no way of using CreateThread relyable with VB6. VB6 creates single threaded apartment applications, which can not create extra threads without problems.
VB Code:
'Using the CreateThread function in Visual Basic 'is very risky! VB5 is 'kinda' stable, but VB6 'applications will probably crash when you 'use the CreateThread function. 'In a form 'Add a command button to the form Private Sub Command1_Click() 'KPD-Team 1999 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] 'After you click this button, try to move the window 'You will see that the AsyncThread-function was executed asynchronously hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID) CloseHandle hThread End Sub Private Sub Form_Unload(Cancel As Integer) 'If the thread is still running, close it If hThread <> 0 Then TerminateThread hThread, 0 End Sub 'In a module Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Public hThread As Long, hThreadID As Long Public Sub AsyncThread() 'Let this thread sleep for 10 seconds Sleep 10000 hThread = 0 End Sub
The CreateThread API call is very dangerous. Many programmer's are misguided about multithreading VB6 applications. Their left to believe that calling the CreateThread API call is all that’s needed to create multithreaded VB6 apps. Ultimately its NEVER reliable because its missing the underlying foundation needed to safely implement multithreading in VB6.
COM objects live in Apartments, to implement multithreading safely you must create a new STA apartment for each new thread you create. You must also create a Message pump for that Apartment so objects can properly communicate and marshal calls. These are only the basics, but its the ONLY safe way to truly implement multithreading in VB6.
My company HalfTime Technologies, Inc. publishes a product called Thread Factory that is specifically designed to safely implement VB6 multithreading. Its completely COM compliant and safe. I invite you to download the evaluation version from our site and see for yourself. The software includes comprehensive HTML help file, tutorials and examples.
http://www.halfx.com.
Thread Factory empowers VB6 programmers to easily create robust multithreaded apps, components and OCX controls. Many Wall street investment banks and universities have licensed our products. Just last week the Astrophysics dept at John Hopkins University licensed our product.
FYI, The download also includes StopWatchPro - a multithreaded stop watch OCX control thats accurate to 1 millisecond.
Thanks,
Michael Miller
:cool:
Way to dig up a post to peddle your own software! :rolleyes: Maybe you and that Katan guy can get together.
Unfortunately I rely mainly on consulting for income. But the Job Market here in NY is terrible so I'm leveraging my software skills through products I created instead. I created some pretty cool stuff IMHO, so hopefully something good will come from it.
I have a wife, 3 kids and a telescope to support.
Love to talk, but I must peddle some more
Best of Luck to You !
Mike :)