防止调用下一个事件处理程序

3

本文介绍了防止调用下一个事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有两个事件处理程序连接到 Windows 窗体中的按钮单击,如下所示:

I have two event handlers wired up to a button click in a Windows form like so:

this.BtnCreate.Click += new System.EventHandler(new RdlcCreator().FirstHandler);
this.BtnCreate.Click += new System.EventHandler(this.BtnCreate_Click);

两者都被正确调用.

但是,是否可以在 FirstHandler() 中阻止 BtnCreate_Click() 被执行?比如:

However is it possible within FirstHandler() to prevent BtnCreate_Click() being executed? Something like:

void FirstHandler(object sender, EventArgs e)
{
   if (ConditionSatisfied)
     //Prevent next handler in sequence being executed

}

我知道我可以取消订阅该事件,但这可以通过编程方式(在方法内)完成吗?

I know I could just unsubscribe the event, but can this be done programmatically (from within the method)?

推荐答案

据我所知,没有解决方案.这是因为无法保证事件发生时调用事件处理程序的顺序.

As far as I know there is no solution for this. That's because there is no guarantee for the order in which the event handlers are called when the event happens.

因此,您不应该以任何方式依赖他们的订单.

Because of that you are not supposed to rely on their order in any way.

这篇关于防止调用下一个事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14