多次附加事件处理程序 [英] Attaching an event handler multiple times

查看:231
本文介绍了多次附加事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手.我只是想知道多次附加事件处理程序是否会导致意外的结果?

I am new to C#. I just wanted to know whether attaching event handler multiple times can cause unexpected result?

实际上,在我的应用程序中,我将事件处理程序附加到类似

Actually in my application i am attaching an event handler to an event like

cr.ListControlPart.Grid.CurrentCellActivated += new EventHandler(Grid_CurrentCellActivated); 

并且此行代码在代码中多次调用.

and this line of code called multiple times in the code.

推荐答案

如果您继续附加事件处理程序,则每次附加处理程序时都会引发一次.这意味着:

If you keep attaching event handlers then it will be raised once for each time you've attached the handler. This means:

  • 如果只需要将其引发一次,则分配一个处理程序.
  • 如果将同一处理程序附加4次,则将被调用4次.

查看您的代码,而不是多次陷入CurrentCellActivated事件中,一次订阅一个常规CellActivated事件将更有意义.

Looking at your code, instead of hooking into the CurrentCellActivated event multiple times it would make more sense to subscribe to a general CellActivated event once.

这篇关于多次附加事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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