[英]How can I know if a .net event is already handled?
I've written some code to handle an event as follows:我编写了一些代码来处理事件,如下所示:
AddHandler myObject.myEvent, AddressOf myFunction
It seemed that everything was working at first, but when I ran the debugger, I discovered that oftentimes, myFunction
would run several times each time myObject.myEvent
fired.一开始似乎一切正常,但是当我运行调试器时,我发现很多时候myFunction
会在每次myObject.myEvent
触发时运行几次。 I figured out that I had allowed the code to add the event handler to run more than once, resulting in this behavior.我发现我允许代码添加事件处理程序多次运行,从而导致这种行为。
Is there a way I can do something like this?有没有办法我可以做这样的事情?
If myObject.myEvent is not handled Then
AddHandler myObject.myEvent, AddressOf myFunction
End If
Assuming it's not your code that's publishing the event, you can't.假设发布事件的不是你的代码,你不能。 The idea is that subscribers are isolated from each other - you can't find out about other event subscribers, raise the event yourself etc.这个想法是订阅者彼此隔离 - 您无法了解其他事件订阅者,自己引发事件等。
If the problem is that you're adding your own handler multiple times, you should be able to fix that yourself by keeping track of whether you have added a handler.如果问题是您多次添加自己的处理程序,您应该能够通过跟踪您是否添加了处理程序来自行解决该问题。 Steven's idea of removing the handler before adding it is an interesting workaround: it's valid to attempt to remove a handler even when it isn't subscribed. Steven 在添加之前删除处理程序的想法是一个有趣的解决方法:即使在未订阅的情况下尝试删除处理程序也是有效的。 However, I'd regard this as a workaround to your app not really knowing what it should be doing.但是,我认为这是您的应用程序不知道它应该做什么的一种解决方法。 It's a very quick short-term fix, but I'd be worried about leaving it in for the longer term.这是一个非常快速的短期解决方案,但我担心将其保留更长时间。
Either:任何一个:
Don't add your handler more than once.不要多次添加处理程序。
Attempt to remove the handler just prior to adding it.尝试在添加之前删除处理程序。
I know this is an old post but just wanted to add a solution for those who come looking in this direction...我知道这是一篇旧帖子,但只是想为那些朝这个方向寻找的人添加一个解决方案......
VB.Net creates a special private member variable in the pattern of <YourEvent>Event
that you can then use to test against Nothing. VB.Net 在<YourEvent>Event
的模式中创建一个特殊的私有成员变量,然后您可以使用它来针对 Nothing 进行测试。
Public Event MyClick As EventHandler
Private Sub OnMyClick()
If MyClickEvent IsNot Nothing Then
RaiseEvent MyClick(Me, New EventArgs())
Else
' No event handler has been set.
MsgBox("There is no event handler. That makes me sad.")
End If
End Sub
Answer sourced from here: Determine if an event has been attached to yet来自此处的答案: 确定是否已附加事件
There's no way to tell that a handler is already attached but you can safely call RemoveHandler on the event before calling AddHandler.无法判断处理程序是否已附加,但您可以在调用 AddHandler 之前安全地对事件调用 RemoveHandler。 If there isn't already a handler, RemoveHandler will have no effect.如果还没有处理程序,RemoveHandler 将不起作用。
Remove the handler and then add it.删除处理程序,然后添加它。 This way it will never be duplicated.这样它就永远不会被复制。 Beware of the null reference error if your object does not exist.如果您的对象不存在,请注意空引用错误。 I got caught on that too and may happen when you are removing the handler outside the sub where the handler is created.我也发现了这一点,当您在创建处理程序的子程序之外删除处理程序时可能会发生这种情况。
if not myObject is nothing then RemoveHandler myObject.myEvent, AddressOf myFunction
if not myObject is nothing then AddHandler myObject.myEvent, AddressOf myFunction
将您的事件处理程序结果保存到数据库/会话中,然后再次读取它们以检查事件是否已被处理。
I know I am a few years late to the game but you could always scope a class variable and then set it after the fact.我知道我玩这个游戏晚了几年,但你总是可以定义一个类变量的范围,然后在事后设置它。 This is not a totally hardened way of doing things but it is better than just hoping you did not have something or re adding it every time.这不是一种完全固化的做事方式,但它比只是希望您没有某些东西或每次都重新添加它要好。 In my case I used this in a WinForms app were I wanted to add a handler for dragging and dropping onto a datagridview surface.在我的情况下,我在 WinForms 应用程序中使用了它,我想添加一个处理程序来拖放到 datagridview 表面上。 I wanted to stop this functionality if part of another datagridview was not yet filled out completely that it was dependent on.如果另一个 datagridview 的一部分尚未完全填写它所依赖的内容,我想停止此功能。
So it would be like this:所以它会是这样的:
Class level班级
Private _handlersAdded As Boolean = False
Constructor:构造函数:
Public Sub New()
AddHandler dgv.DragEnter, AddressOf DragEnter
_handlersAdded = True
End Sub
Method that determines issue:确定问题的方法:
Private Sub CheckRowsAreDone()
For Each row As DataGridViewRow In dgv.Rows
Dim num = 0
For i = 0 To row.Cells.Count - 1
Dim val = If(Not String.IsNullOrEmpty(row?.Cells(i)?.Value?.ToString), 1, -1)
num += val
Next
If num > -(row.Cells.Count) And num < (row.Cells.Count) Then
RemoveHandler dgv.DragEnter, AddressOf DragEnter
_handlersAdded = False
Exit Sub
End If
If Not _handlersAdded Then
AddHandler dgv.DragEnter, AddressOf DragEnter
_handlersAdded = True
End If
Next
End Sub
You may use IsHandleCreated
property to check your event already has an handle or not.您可以使用IsHandleCreated
属性来检查您的事件是否已经有一个句柄。
If e.Control.IsHandleCreated = False Then
AddHandler e.Control.KeyPress, AddressOf TextBox_keyPress
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.