How can I know if a .net event is already handled?

孤街浪徒 提交于 2019-11-30 13:54:42

问题


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. 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

回答1:


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. 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.




回答2:


Either:

  1. Don't add your handler more than once.

  2. Attempt to remove the handler just prior to adding it.




回答3:


There's no way to tell that a handler is already attached but you can safely call RemoveHandler on the event before calling AddHandler. If there isn't already a handler, RemoveHandler will have no effect.




回答4:


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.

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




回答5:


Save your event handler results to the database/session and then read them in again to check if event has already been handled.




回答6:


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. I wanted to stop this functionality if part of another datagridview was not yet filled out completely that it was dependent on.

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



回答7:


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



回答8:


You may use IsHandleCreated property to check your event already has an handle or not.

  If e.Control.IsHandleCreated = False Then
            AddHandler e.Control.KeyPress, AddressOf TextBox_keyPress
  End If


来源:https://stackoverflow.com/questions/3477303/how-can-i-know-if-a-net-event-is-already-handled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!