Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 132
  • Last Modified:

Class Event Lifetime

I have a third party COM product which triggers events and works fine in VBA.
Porting to VB6 I have difficulty because the old problem of asynchronous execution means the event class terminates before the Class events I am trying to trap have triggered.
ie xyzCompleteEvent never traps because the sub calling the process completes and terminates the class before xyz even gets underway.
If I add a timing loop, all is well but I can't live with that.
Register_event_Handler is being called from the same sub that calls the process I am trying to trap.
I remember reading somewhere of the ability to make the class global or some such.
Any help appreciated.
   
0
IanDB
Asked:
IanDB
  • 2
1 Solution
 
webJoseCommented:
Ok, sounds like you have a COM server that provides an object capable of asynchronous processing, probably accomplished by multithreading.  What you want is start the process and do not leave the starting procedure until the task being done in the worker thread (I assume) is done, which is signaled by the xyzCompleteEvent.  Am I correct?  Well, if so, I don't see much gain in the component using multithreading, but a code like the one below should do the trick.

Private Const INFINITE As Long = &HFFFFFFFF

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (byval lpEventAttributes As long, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
Private Declare Function SetEvent Lib "kernel32" (ByVal hEvent As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

private hCompleteEvent as long

Private Sub SomeObject_xyzCompleteEvent()
    setevent hcompleteevent
end sub

private sub StartTheWork()
    hcompleteevent = createevent(0, 0, 0, "_MyCompleteEvent_")
    someobject.startjob
    waitforsingleobject hcompleteevent, infinite
    closehandle hcompleteevent
    'The rest of code here, like Unload Me or sumtin
end sub

When you call StartTheWork(), the object will start working, then you wait using the WaitForSingleObject API function until the _MyCompleteEvent_ event gets signaled in the xyzCompleteEvent.  Once signaled, the code in StartTheWork() resumes execution.
0
 
CleanupPingCommented:
IanDB:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
webJoseCommented:
Again, I think my answer is correct and I should get the points awarded.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now