0

I have an ActiveX control in a Delphi project. It features a function that calculates certain value. The trouble is that the result of calculation is provided in an event that is triggered asynchronously.

I would rather prefer to make a call to this function and to wait until it finishes, then to proceed with the program (sync call).

I tried using a thread, but that did not trigger the event due to unknown reason. Furthermore, I am not sure if it is thread safe to use the control in this way.

Are there any general guidelines how to safely convert an AX async method to a sync method?

Adrian Wragg
  • 7,066
  • 3
  • 26
  • 50
Zoran
  • 153
  • 1
  • 1
  • 7
  • I have finally managed to solve the issue in a different way than using TEvent. The ActiveX vendor has guided me to the set of functions which return sync result if combined in a particular way. I also tried the suggestion with TEvent class and used some examples from the internet and that worked ok too. Thanks for pointing me to that area- TEvent is a very useful class indeed. – Zoran Feb 24 '14 at 10:53

1 Answers1

4

Create a TEvent that is initially unsignaled. Signal it in the asynchronous event handler. Wait for it to become signaled after starting the asynchronous operation. Depending on the COM object's threading model, you might need to pump the message queue periodically to get the asynchronous event to trigger.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • This is what I tried: [code] MyEvent.ResetEvent; MyActiveXControl.GenerateValue(); cnt := 0; while MyEvent.WaitFor(50) <> wrSignaled do begin Application.ProcessMessages; inc(cnt); if cnt > 100 then break; end; [/code] And what happens is that sometimes I get the ActiveX control fire the event as I expected, but often the loop above expires and I exit the routine and then the AX fires the event. I am interested in knowing how to properly "pump" the message queue? – Zoran Feb 19 '14 at 17:54
  • You should have updated your question with the code you tried. Also, when dealing with a message queue, you should use `MsgWaitForMultipleObjects()` instead of `TEvent.WaitFor()` so you can detect WHEN to call `Application.ProcessMessages()`. Or, when dealing with COM objects, use `CoWaitForMultipleHandles()`. – Remy Lebeau Feb 19 '14 at 19:42