Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB6 & multi-threading?

Posted on 1999-07-05
11
Medium Priority
?
664 Views
Last Modified: 2008-03-06
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!
0
Comment
Question by:SLE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 1521994
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..
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1521995
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

0
 
LVL 2

Author Comment

by:SLE
ID: 1521996
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!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Expert Comment

by:Mirkwood
ID: 1521997
Have you read the URL's.
It's not supported anymore.
0
 
LVL 2

Author Comment

by:SLE
ID: 1521998
>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.
0
 
LVL 2

Author Comment

by:SLE
ID: 1521999
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.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1522000
I moved everything to C++ and called it from VB
0
 
LVL 2

Author Comment

by:SLE
ID: 1522001
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.


0
 
LVL 6

Expert Comment

by:VBGuru
ID: 1522002
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.
0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 400 total points
ID: 1522003
Summary:
- 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..
0
 
LVL 2

Author Comment

by:SLE
ID: 1522004
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.



0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

671 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question