views:

297

answers:

1

COM Object (Server) sends event notification successfully to COM Client

Without:

  • ATL
  • MFC

How to efficiently get the main thread to wait/sleep (infinitely) until COM Server notifies the COM Client of a particular event?

+2  A: 

With event objects.

The main thread calls CreateEvent() in its initialisation to create an auto-reset event object.

The main thread then enters an event loop in which it calls MsgWaitForMultipleObjects() repeatedly. (here is an example of a message loop.)

And you generally do need to check for window messages, even if the main thread has no GUI.

In the client thread (the one that creates the sink object) call SetEvent() within the sink method, after any necessary state update. This will wake up the main thread.

And read this and this, if you haven't already.

finnw
@finnw: Will I have to use STA? The first (main) call to CoInitializeEx(STA) will call CreateEvent() and enter MsgWaitForMultipleObjects(). Will I create a another thread: CoInitializeEx(STA) to then call/create an instance of the sink object on event notification to then call SetEvent()?
Aaron
@Atklin, that depends on the requirements of the source object. The event loop can run in either STA or MTA.
finnw
@finnw: I've tried both and it works (according to OLEVIEW - the COM Server is "LocalServer32"). I'm I write in saying I need to create another thread? or from the main STA: create an instance of the sink object, call the relavent method, wait (event loop) until notified (as in above comment)...
Aaron