简体   繁体   English

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

[英]How to deregister correctly from receiving notifications using IConnectPoint Unadvise?

In the documentation for Mobile Broadband API , it says: Mobile Broadband API文档中 ,它说:

The following procedure describes how to register for notifications. 以下过程描述了如何注册通知。

1.Get an IConnectionPointContainer interface by calling QueryInterface on an IMbnInterfaceManager > object. 1.通过在IMbnInterfaceManager>对象上调用QueryInterface获取IConnectionPointContainer接口。
2.Call FindConnectionPoint on the returned interface and pass IID_IMbnPinEvents to riid. 2.在返回的接口上调用FindConnectionPoint并将IID_IMbnPinEvents传递给riid。
3.Call Advise on the returned connection point and pass a pointer to an IUnknown interface on an > object that implements IMbnPinEvents to pUnk. 3.在返回的连接点上调用Advise,然后将指针传递给在将IMbnPinEvents实现为pUnk的>对象上的IUnknown接口。

Notifications can be terminated by calling Unadvise on the connection point returned in step 2. 可以通过在步骤2中返回的连接点上调用Unadvise来终止通知。

I have got some code that carries out the first 3 steps, and it successfully registers for MBN events. 我有一些执行前三个步骤的代码,它成功注册了MBN事件。 However, now I need to de-register temporarily from receiving these events. 但是,现在我需要暂时取消注册以接收这些事件。
So, after a couple of first attempts that ended with COM exceptions, I tried the following code (with try/catch blocks): 因此,在几次以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);
}

When I run this code, I get no errors or exceptions from MBN. 当我运行此代码时,我没有从MBN收到任何错误或异常。 But the event handlers aren't de-registered. 但是事件处理程序并未取消注册。 I can still see in the log files, MBN events arriving and being handled. 我仍然可以在日志文件中看到MBN事件到达并正在处理。

Can anyone tell me what I am missing? 谁能告诉我我想念的东西吗? Thank you. 谢谢。

I think I figured out the problem. 我想我已经解决了问题。 I have many different types of GUIDs, and I thought they all used the same IConnectionPoint, so I saved it to the same object in the RegisterEvent function. 我有许多不同类型的GUID,我认为它们都使用相同的IConnectionPoint,因此我将其保存到RegisterEvent函数中的同一对象中。

When I tried creating a new IConnectionPoint object for each GUID, and saving each IConnectionPoint separately, the DeregisterEvent function also worked properly. 当我尝试为每个GUID创建一个新的IConnectionPoint对象并分别保存每个IConnectionPoint时,DeregisterEvent函数也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM