Question

I am working with a proprietary VB6 COM library. The library has some functions which fire and I get the results once complete through Events.

Container Class:

Private WithEvents myObj As proprietaryObj
Public status as Integer
Set myObj = new proprietaryObj
status = 1
Call myObj.DoIt1()

...

' Call back event function
Private Sub myObj_Done(ByVal Code As Long)
    ...
    MsgBox "Finished"
    status = 2
End Sub

So everything works well (verified). What I want to do is encapsulate the above code + more in a class, so that I wrap multiple functions which need to stack, or get executed consecutively after a successful callback report.

So I went ahead and did this:

Call myObj.DoIt1()
Do
    If myObj.Status = 2 Then Exit Do
    If myObj.Status = -1 Then Exit Do 'Error
    DoEvents
Loop
call myObj.DoIt2()

I get the "Finished" dialog box, but DoIt2 never fires. In fact, if I pause my code while running debug after I see the "Finished" message, the code is currently executing the DoEvents, and the myObj.Status = 1 as if it were never touched.

It almost seems as if a different instance of the object was created for a separate thread? How do I safely and correctly wait for the event callback to fire before continuing execution with the next DoIt2() ?

Was it helpful?

Solution

You need to use your local status variable, because that is what you are setting to 2:

Call myObj.DoIt1()
Do
    If status = 2 Then Exit Do
    If status = -1 Then Exit Do 'Error
    DoEvents
Loop
call myObj.DoIt2()

Else you could also try to set status = 2 before the call to MsgBox "Finished".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top