阻止事件处理程序从另一个事件处理程序接收事件 [英] Stop an Event Handler from Receiving Event from another Event Handler

查看:62
本文介绍了阻止事件处理程序从另一个事件处理程序接收事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个按钮有两个事件处理程序,我想检查第一个处理程序中的条件,如果该条件为true,则可以取消另一个处理程序的执行.

I have two event handlers for one button and I want to check a condition in the first handler and if it is true, be able to cancel the other handler from executing. Is this possible?

推荐答案

我能想到的唯一方法是创建/设置一个变量,以指示应该忽略第二个事件.当然,您也可以重构代码,使之有意义.
The only way I can think of is to create/set a variable to indicate that the 2nd event should be ignored. Of course, you could also refactor your code so that it made sense.


尽管.Net事件处理程序按照注册时的顺序执行

请参阅委托的调用列表是一组有序的委托,其中列表的每个元素都精确调用委托代表的方法之一.调用列表可以包含重复的方法.在调用过程中,将按顺序调用方法委托试图在其调用列表中出现;委托尝试调用其调用列表中的每个方法;重复项每次出现在调用列表中都被调用一次;委托是不可变的;一旦创建,委托的调用列表就不会改变."来自 MSDN委托类 [
Although .Net event handlers are executed in the order they are registered

See "The invocation list of a delegate is an ordered set of delegates in which each element of the list invokes exactly one of the methods represented by the delegate. An invocation list can contain duplicate methods. During an invocation, methods are invoked in the order in which they appear in the invocation list. A delegate attempts to invoke every method in its invocation list; duplicates are invoked once for each time they appear in the invocation list. Delegates are immutable; once created, the invocation list of a delegate does not change." from MSDN Delegate Class[MSDN Delegate Class]

This is an implementation detail rather than a specification so it could change in the future, and as such you can''t rely on this behavior, so any coding that relies on this is inherently dangerous, so as stated by John, you need refactor the code


为什么将按钮连接到两个处理程序?您的第一个处理程序中的代码应该可以建立一个可以完成该处理程序工作的方法,然后确定是否需要执行包含您在第二个处理程序中具有的代码的第二个方法.
Why do you have the button wired up to two handlers then? The code in your first handler should cann a method that does the work of that handler, then determine if it needs to execute a second method containing the code you have in the second handler.


这篇关于阻止事件处理程序从另一个事件处理程序接收事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆