tags:

views:

135

answers:

2

Without:

  • ATL
  • MFC

Note:

  • Client is executing in a different thread to that of the server

Question:

  • How do I control the behaviour of the client, once an event notification is received from the COM object (Server)?
  • How to implement an event interface from client?

Illustration below:

hresult = pis8->QueryInterface(
          __uuidof(IConnectionPointContainer),
   (void **) &pContainer);

//result handling omitted


hresult = pContainer->FindConnectionPoint(
       __uuidof(IS8SimulationEvents),
       &pConnection);

//result handling omitted
A: 

If I'm understanding the question right, seems like the client needs to be running a waitloop, something like

while(!threadCancel)
{
    DWORD waitResult = WaitForMultipleObjects(actionCount, waitHandles, FALSE, 500);
    switch (waitResult)
    {
        case SERVER_COMMAND_1:
            HandleServerCommand1();
            break;
        ...etc...
        default:
           throw ...
     }
}

The client's event sinks trigger the wait handles, effectively allowing the server to tell the client what to do.

Aidan Ryan
+1  A: 

The client implements the event interface (IS8SimulationEvents) This can be in a separate component, or on the client component itself. The implementation is called when the component fires an event.

After FindConnectionPoint, the client calls pConnection->Advise, passing the IS8SimulationEvents and receiving a "cookie". The cookie is required to call Unadvise, which must be called during cleanup to disconnect.

If the client runs in a different thread than server, the client needs to run a message loop to receive calls.

peterchen
How to implement a message loop efficiently?
Aaron
see here: http://en.wikipedia.org/wiki/Message_loop_in_Microsoft_WindowsYOu can quit this by PostQuitMessage, or adding another mechanism to the loop. If you need to mix message handling with waiting for events/similar things, use MsgWaitForMultipleObjects/Ex in the loop
peterchen