[英]How to deregister correctly from receiving notifications using IConnectPoint Unadvise?
在Mobile Broadband API的文档中 ,它说:
以下过程描述了如何注册通知。
1.通过在IMbnInterfaceManager>对象上调用QueryInterface获取IConnectionPointContainer接口。
2.在返回的接口上调用FindConnectionPoint并将IID_IMbnPinEvents传递给riid。
3.在返回的连接点上调用Advise,然后将指针传递给在将IMbnPinEvents实现为pUnk的>对象上的IUnknown接口。可以通过在步骤2中返回的连接点上调用Unadvise来终止通知。
我有一些执行前三个步骤的代码,它成功注册了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事件到达并正在处理。
谁能告诉我我想念的东西吗? 谢谢。
我想我已经解决了问题。 我有许多不同类型的GUID,我认为它们都使用相同的IConnectionPoint,因此我将其保存到RegisterEvent函数中的同一对象中。
当我尝试为每个GUID创建一个新的IConnectionPoint对象并分别保存每个IConnectionPoint时,DeregisterEvent函数也可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.