簡體   English   中英

如何使用IConnectPoint Unadvise從接收的通知中正確注銷?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM