COM事件通知

时间:2009-01-17 23:41:51

标签: c++ com

  • ATL
  • MFC

注意:

  • 客户端正在不同的线程中执行服务器

问题:

  • COM对象收到事件通知后,如何控制客户端的行为 >(服务器)?
  • 如何从客户端实现事件接口?

下图:

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

//result handling omitted


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

//result handling omitted

2 个答案:

答案 0 :(得分:1)

客户端实现事件接口(IS8SimulationEvents)这可以在单独的组件中,也可以在客户端组件本身上。当组件触发事件时,将调用该实现。

FindConnectionPoint之后,客户端调用pConnection->Advise,传递IS8SimulationEvents并接收“cookie”。 cookie需要调用Unadvise,必须在清理期间调用以断开连接。

如果客户端在与服务器不同的线程中运行,则客户端需要运行消息循环来接收呼叫。

答案 1 :(得分:0)

如果我理解正确的问题,似乎客户需要运行waitloop,比如

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

客户端的事件接收触发等待句柄,有效地允许服务器告诉客户端要做什么。