Re: Problem with Multithread in VB6



I have a DLL written in C# and exposing COM interface
(ComInterfaceType.InterfaceIsDual.)
Events get raised by this DLL on their own threads (say from the Threadpool.)

In a VB6 client application, if I'll create an object "WithEvents" and write
an event handler, can I safely update GUI directly in this handler?


If not what would be your recommendations, tips, examples...?

My preference would be in VB6 event handler to collect received from the
event data in a collection and later to manage them in a Timer.
But how to lock the collection in VB6?


Thanks,

Valentina



"Tony Proctor" wrote:

An ActiveX EXE doesn't just "simulate" multi-threading Mike. It can be truly
multi-threaded. Check out the 'Thread-per-Object' and 'Thread-Pool' project
properties.

VB6 works fine in STA type multi-threaded environments. This ActiveX EXE
mechanism is just one example of such an environment. Others include
MTS/COM+, and even IIS. Yes, when you use a VB6 DLL from ASP, it will be
executing on multiple ASP threads at the same time.

Although there are books showing how to get VB6 to work in an MTA type
multi-threaded environment, this is generally acknowledged to be a lot of
work. Plus you're in completely unsupported territory.

Tony Proctor

"Mike Sharpe" <MikeSharpe@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:2607D4DC-B8F6-4A79-90AB-BBA04CAAB991@xxxxxxxxxxxxxxxx
VB is not multithreaded and the process in which you are attempting to
create
threads is incorrect. There are some APIs that you can use but they can
cause some serious problems. The only safe way to simulate multi-threaded
behavior is to use the ActiveX EXE project type. This is similar to a DLL
but that it launches in a seperate process. This allows it to process
while
your main application processes as well.

"Crevit" wrote:

Hi friends,



I have tryed to create thread in VB6 but with few results



I know that VB6 doesn't support, in native kind, multithreading but I
have
read that if I create apposite classes I can have good results



I have create thread with 2 classes: first class, Dispatcher
(instancing=public not creatable) manage my thread and then, the second
class that identifies my thread, clsThread(instancing=public multiuse)



This is my Sub Main used to create the main thread:



----------------------------

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _

(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function GetWindowThreadProcessId Lib "user32" _

(ByVal hwnd As Long, lpdwProcessId As Long) As Long

Declare Function EnumThreadWindows Lib "user32" _

(ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long)
_

As Long



Sub Main()



Dim frmMainForm As DispMain



Call EnumThreadWindows(App.ThreadID, AddressOf EnumThreadWndMain,
0&)



If mhwndVB = 0 Then

Err.Raise ERR_InternalStartup + vbObjectError, , _

"Internal error starting thread"

Else

Call GetWindowThreadProcessId(mhwndVB, mlngProcessID)



If FindWindow(vbNullString, CAPT_PROCESS & CStr(mlngProcessID))
= 0
Then



' This windows has not been found, so this process is the
first

If App.StartMode = vbSModeStandalone Then



' Load a hidden form with a unique caption

mfrmForm.Caption = CAPT_PROCESS & CStr(mlngProcessID)



' The event Initialize of clsDispMain (Instancing =

' PublicNotCreatable) displays the main user interface

Set frmMainForm = New DispMain

Else

Err.Raise ERR_NoAutomation + vbObjectError, , _

"Application can't be started with Automation"

End If

End If

End If



End Sub




The Dispmain class, with the function crerateobject("clsThread")
instances
object of clsThrread type and the adds them in a collection associating
at
every object an ID.



The clsThread class instances,on the initialize, a form with a button
and a
text box

when I click the button a cycle starts and in the text box the seconds
showed,





but when i start 2 forms and press the 2 buttons, a cycle stops and when
one
finishes start the other.



In what kind I can create asynchronous multithread? That allows to
execute
2 job together in parallel kind?



Thanks in advances.

Crevit






.