C# 如何使用IConnectPoint Unadvise正确注销接收通知?
在报告中,它说: 以下过程描述了如何注册通知 1.通过调用IMbnInterfaceManager>对象上的QueryInterface来获取IConnectionPointContainer接口。 2.在返回的接口上调用FindConnectionPoint,并将IID_imbInputEvents传递给riid。 3.在返回的连接点上调用advice,并在一个>对象上传递一个指向IUnknown接口的指针,该对象将IMBNPINETENTS实现为pUnk 可以通过在步骤2中返回的连接点上调用Unadvise来终止通知 我有一些执行前3个步骤的代码,它成功地注册了MBN事件。 但是,现在我需要暂时取消注册以接收这些事件。 因此,在以COM异常结束的几次首次尝试之后,我使用try/catch块尝试了以下代码:C# 如何使用IConnectPoint Unadvise正确注销接收通知?,c#,com,iconnectionpoint,mobile-broadband-api,C#,Com,Iconnectionpoint,Mobile Broadband Api,在报告中,它说: 以下过程描述了如何注册通知 1.通过调用IMbnInterfaceManager>对象上的QueryInterface来获取IConnectionPointContainer接口。 2.在返回的接口上调用FindConnectionPoint,并将IID_imbInputEvents传递给riid。 3.在返回的连接点上调用advice,并在一个>对象上传递一个指向IUnknown接口的指针,该对象将IMBNPINETENTS实现为pUnk 可以通过在步骤2中返回的连接点上调用
//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函数也正常工作