简体   繁体   English

C#禁用事件处理程序问题

[英]C# Disable Event Handler Problem

i am using textchanged event and i disable it where i don't need as following 我正在使用textchanged事件,我禁用它,我不需要如下所示

object.Event -= new System.EventHandler(myHandler);
//my code which doesn't need event handler
object.Event += new System.EventHandler(myHandler);

i used many times like this. 我曾多次使用这样的话。 but i needed sometimes 2 disable code like this: 但我有时需要2个禁用代码,如下所示:

object.Event -= new System.EventHandler(myHandler);
object.Event -= new System.EventHandler(myHandler);

of course i finished it with 2 enable code 当然我用2启​​用代码完成了它

object.Event += new System.EventHandler(myHandler);
object.Event += new System.EventHandler(myHandler);

i don't know yet why i needed 2 times remove event handler but it worked great. 我不知道为什么我需要2次删除事件处理程序,但它工作得很好。

but in 1 case i got problem. 但在1例中我遇到了问题。

it doesn't work with 2 or more disable code. 它不适用于2个或更多禁用代码。

my question is, how can i watch this eventhandler if it needs just one -= code or more? 我的问题是,如果它只需要一个 - =代码或更多,我怎么能看到这个事件处理程序? or how can i manage it? 或者我该如何管理呢? i always worked like this, to make sure that i always leave event handler as first time 我总是像这样工作,以确保我总是第一次离开事件处理程序

object.Event -= new System.EventHandler(myHandler);
//my code which doesn't need event handler
object.Event += new System.EventHandler(myHandler);

My advice would be to stop removing and re-adding the event handler, and instead add a flag to your event handler itself which inhibits whatever activities you need to inhibit during these sections of code. 我的建议是停止删除并重新添加事件处理程序,而是向事件处理程序本身添加一个标志,该标志禁止在这些代码段中需要禁止的任何活动。

You can either have a single boolean flag, or use some kind of reference count, depending on how you need to cope with nesting. 您可以使用单个布尔标志,也可以使用某种引用计数,具体取决于您需要如何处理嵌套。

If there's some reason why you can't change the existing event handler, what about creating a new event hander which you attach to Event, and call the old one from that? 如果有一些原因导致您无法更改现有的事件处理程序,那么创建一个附加到Event的新事件处理程序,并从中调用旧事件呢?

You need to remove an event handler as many times as you've added it - and you won't be able to tell when that is, as the subscriptions are effectively hidden from you. 您需要在添加事件处理程序时多次删除事件处理程序 - 并且您将无法判断它是什么时候,因为订阅对您有效隐藏。

Ideally, just make sure you only subscribe as many times as you need to, and it should be obvious how many times you need to unsubscribe too. 理想情况下,只需确保您只需要订阅多次,并且很明显您需要多少次取消订阅。 Usually this will be once. 通常这将是一次。 It's somewhat odd that you ended up with two subscriptions to start with... I suspect that may indicate a bug somewhere in your code. 有点奇怪,你最终有两个订阅开始...我怀疑这可能表明你的代码中的某个地方有一个错误。

Note that unsubscribing using an event handler which isn't already subscribed is a no-op. 请注意,使用尚未订阅的事件处理程序取消订阅是无操作。

(Will's idea of the event handler itself knowing whether or not it's really "active" is a good one too, btw.) (Will的想法是事件处理程序本身知道它是否真的“活跃”也是一个好的,顺便说一下。)

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

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