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?

Who is Participating?
MirkwoodConnect With a Mentor Commented:
- CreateThread is possible in VB5
- CreateThread crashes VB6
- Timers are sometimes a solution
- You can use a DLL and let the DLL do the multithreading. This is hard and you have to know what you are doing.
The VB 6.0 Win32API of Bock gives an example of how to do this. This example still crashes my VB6 in some cases though..
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

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

SLEAuthor Commented:
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?

Have you read the URL's.
It's not supported anymore.
SLEAuthor Commented:
>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.
SLEAuthor Commented:

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?


I moved everything to C++ and called it from VB
SLEAuthor Commented:
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.
SLEAuthor Commented:
this might overrun all claims that VB6 is not threadsafe:

Create threads safely in VB6 (8/9/1999)
[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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.