如何使用IConnectPoint Unadvise正确注销接收通知?

时间:2013-05-02 12:25:29

标签: c# com iconnectionpoint mobile-broadband-api

documentation for Mobile Broadband API中,它说:

  

以下过程介绍了如何注册通知。

     

1.通过在IMbnInterfaceManager>上调用QueryInterface获取IConnectionPointContainer接口。对象。
  2.在返回的接口上调用FindConnectionPoint并将IID_IMbnPinEvents传递给riid   3.Call建议返回的连接点并将指针传递给>上的IUnknown接口。将IMbnPinEvents实现为pUnk的对象。

     

可以通过在步骤2中返回的连接点上调用Unadvise来终止通知。

我有一些执行前3个步骤的代码,它成功注册了MBN事件。 但是,现在我需要暂时取消注册以接收这些事件 因此,在以COM异常结束的几次首次尝试之后,我尝试了以下代码(使用try / catch块):

//First get the notifications

public void RegisterEvent(object iUnk, Guid guid, out uint storedTag)
{
IConnectionPoint icp = null;
Guid curGuid = guid;
storedTag = 0;
if ((curGuid == typeof(IMbnInterfaceManagerEvents).GUID) )              
{
  // For this event, the connection point is defined on the interface manager object
  m_InterfaceManagerCPC.FindConnectionPoint(ref curGuid, out icp);
  // Call Advise on the connection point to register
  icp.Advise(iUnk, out storedTag);
  //Save the IConnectionPoint object
  interfaceManagerCP = icp;

}

//Now deregister the events

public void DeregisterEvent(Guid guid, uint storedTag)
{
IConnectionPoint icp = null;
Guid curGuid = guid;

// Find the appropriate connection point to call Unadvise on
if ((curGuid == typeof(IMbnInterfaceManagerEvents).GUID) )              
{
  // Call Unadvise on the saved connection point to de-register
  interfaceManagerCP.Unadvise(storedTag);
}

当我运行此代码时,我没有从MBN获得任何错误或异常。但是事件处理程序没有取消注册。我仍然可以在日志文件中看到MBN事件到达并正在处理。

谁能告诉我我失踪了什么?谢谢。

1 个答案:

答案 0 :(得分:0)

我想我弄明白了这个问题。我有许多不同类型的GUID,我认为它们都使用了相同的IConnectionPoint,因此我将它保存到RegisterEvent函数中的同一个对象。

当我尝试为每个GUID创建一个新的IConnectionPoint对象并分别保存每个IConnectionPoint时,DeregisterEvent函数也能正常工作。