0

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() ?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Authman Apatira
  • 3,994
  • 1
  • 26
  • 33
  • 2
    Breaking into the code will almost certianly pause on the DoEvents as that is when it will receive the break message. Ctrl+Break will be recieved anywhere and could break anywhere else. – Deanna Nov 22 '11 at 09:22

1 Answers1

4

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".

MicSim
  • 26,265
  • 16
  • 90
  • 133