本文介绍了建议一个HTML控件:我应该什么时候调用DispEventUnadvise?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在BHO中使用 DispEventAdvise 以捕获特定复选框的 onclick 事件。您可以在中看到代码(虽然它

I use DispEventAdvise in a BHO in order to capture onclick events of a specific checkbox. You can see the code in my previous question (although it's not that relevant).

为了以后能够调用 DispEventUnadvise ,我保留了对 IHTMLElement 复选框的对象。

In order to be able to call DispEventUnadvise later, I keep a reference to the IHTMLElement object of the checkbox.


  1. $ c> DispEventUnadvise ?有没有办法知道复选框何时消失?

  1. When should I call DispEventUnadvise? Is there a way to know when the checkbox is going away?

保存对 IHTMLElement object?我的意思是,当页面被销毁,并且仍然有一个引用对象的复选框,它会发生什么?

Is it even legal to keep a reference to the IHTMLElement object? I mean, when the page is destroyed, and there's still a reference to the object of that checkbox, what happens to it?


推荐答案

当您不再想要接收事件时,您需要从事件源/连接点取消。调用,特别是使连接点释放你的接收器接口指针。在取消之前,连接点持有一个引用并扩展你的sink对象的生命周期。

You are expected to unadvise from event source/connection point when you no longer want to receive events. The call, in particular, make the connection point release your sink interface pointer. Before unadvising, connection point holds a reference and extends your sink object lifetime.

也就是说,任何时候你想选择退出,没有特定的时间你应该在哪里做,除了一般的考虑。

That is, you call it any time you want to opt out, there is no specific good time where you should do it at, other than general considerations.

问题的另一部分当复选框离开时但是不直接相关。连接到连接点,您不会收到离开通知。即使整个页面消失,保持一个接口指针是合法的:复选框,单独或与其所有者,将保持在终止状态,直到所有外部引用(包括您的)被释放。作为安全终止的一部分,当页面/文档消失时,它可能从其侧面剥离连接点连接,因为没有事件将被跟随,并且文档正在进行健康清理以避免循环引用和泄漏。

The other part of the question "when checkbox is going away" however is not related directly. Being connected to connection point you don't receive "going away" notification. It is legal to keep holding an interface pointer even if the entire page went away: the checkbox, alone or together with its owner, will remain in terminating state until all external references (including yours) are released. As a part of safe termination, when the page/document goes away it might strip connection point connections from its side because no events are going to follow and the document is doing sanity cleanup to avoid circular references and leaks.

在你的情况下,我想你最好的办法是找到另一个合适的事件,看看整个文档何时消失,并假设复选框也消失了。另一个[不太安全]选项是看看你的接收器接口对象:一旦其外部引用意外地通过连接点释放,这意味着它做清理和复选框会消失。

In your case, I suppose your best way out is to find another suitable event to see when the entire document goes away and assume that checkbox is going away as well. Another [less safe] option is to watch your sink interface object: once its external reference is unexpectedly release by connection point, it means that its doing cleanup and checkbox is going away.

这篇关于建议一个HTML控件:我应该什么时候调用DispEventUnadvise?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 00:19