Link to home
Start Free TrialLog in
Avatar of SLE
SLE

asked on

VB6 & multi-threading?

Is there a simple, straight-forward way of creating a multi-threaded VB6 application? I've run into several examples using classes and/or API, even a control (ActiveThread), but none for VB6 (some even claim VB6 isn't thread-safe).

I "simply" want to have several concurrent timers in my application - this can't be that hard, can it?

Thx!
Avatar of caraf_g
caraf_g

Interesting... I've heard this too. It's been claimed that certain support for threads that was available in VB5 has been removed for VB6. Keep me informed..
See http://vb.duke.net/threads.html
or read the stuff below for another example

Here is an example from http://www.hilonet.com/vbthread/
The code
1.Assuming that we have an application that among other things can copy a file on the click of a button.

2.The copy button could have code like this:

Private Sub cmdCopyFile_Click()
Dim lpThreadID As Long
Dim hThread As Long

'spawn a new thread of execution starting with the AsyncFileCopy function
hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncFileCopy, ByVal 0&, ByVal 0&, lpThreadID)
CloseHandle hThread 'clean up after ourselves
End Sub


3.Code this for you module:

Option Explicit

Public gsOriginalFile As String
Public gsDuplicateFile As String
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 CloseHandle Lib "kernel32" (ByVal hHandle As Long) As Long

Public Function AsyncFileCopy() As Long
'Assuming that gsOriginalFile & gsDuplicateFile
'are valid path/file names set elsewhere.

FileCopy gsOriginalFile, gsDuplicateFile
End Function

See also
http://www.microsoft.com/msj/0897/multithreading.htm
http://support.microsoft.com/support/kb/articles/q198/6/07.asp
http://support.microsoft.com/support/kb/articles/q196/0/26.asp

Avatar of SLE

ASKER

Thank you Mirkwood - I've examined the code and URLs you've answered my question with (I already did before posting the question), but have you tested this code with VB6?

The following application crashes with a Dr. Watson Fatal Error ("Could not attach to the application, Windows NT Error Code=87"). The application consists of 1 module only:

Option Explicit

Public Declare Function CreateThread Lib "kernel32" _
        (lpThreadAttributes As Any, _
         ByVal dwStackSize As Long, _
         lpStartAddress As Long, _
         lpParameter As Any, _
         ByVal dwCreationFlags As Long, _
         lpThreadId As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Sub Main()
    Dim lpThreadId As Long
    Dim hThread As Long
   
    hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf Test, ByVal 0&, ByVal 0&, lpThreadId)
    CloseHandle hThread
End Sub

Public Function Test() As Long
    Open App.Path & "\thread.log" For Append As #1
    Print #1, Now
    Close #1
End Function

I'm using VB6SP3 on a NTWS4.0SP5 machine - what's going on here?

Thx!
Have you read the URL's.
It's not supported anymore.
Avatar of SLE

ASKER

>It's not supported anymore

This *WAS* my point, right. So, I'll have to get back to VB5? Great. Impossible.

I'll leave the question open for additional answers/comments.
Avatar of SLE

ASKER

FYI:

I just tested the above code on a different (clean) NTWS4.0SP3 machine with VB5SP3 - it crashes with the same error.

Should I simply forget all about VB and threads?


Desparate,

SLE.
I moved everything to C++ and called it from VB
Avatar of SLE

ASKER

That's what we'll do I guess... :o(

The latest information I got claims that VB5 works fine with threads *most of the time* - it *NEVER* works with VB6 though... Work-around could be using COM...

Thanks to all - Mirkwood: do post an answer and I'll give you the points.


check out this book.
Visual Basic 6.0 WIN 32 API - Tutorial      by Jason Bock. In this book he talks about
>>I "simply" want to have several concurrent timers in my application - this can't be that hard, can it?

The solution ofcourse he uses activex Dll for multithreading. No book till now i have found uses CreateThread API to create threads in VB.
ASKER CERTIFIED SOLUTION
Avatar of Mirkwood
Mirkwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SLE

ASKER

this might overrun all claims that VB6 is not threadsafe:

Create threads safely in VB6 (8/9/1999)
http://vbwire.com/nl?1969
[article] Jason Bock, author of "Visual Basic 6 Win32 API Tutorial," has published an article on his web site that documents a free component called ThreadLauncher.  This component allows a developer to create threads safely in VB6.  Check out the programming section for more details.