PC Review


Reply
Thread Tools Rate Thread

Checking for event handlers

 
 
Darren
Guest
Posts: n/a
 
      18th Aug 2005
Is there any way to check to see if there are any handlers registered
for a given event?
 
Reply With Quote
 
 
 
 
Damien
Guest
Posts: n/a
 
      19th Aug 2005
Darren wrote:
> Is there any way to check to see if there are any handlers registered
> for a given event?


Well, I have something in VB, hope this helps:

Public Class Blah
Public Shared Event InstanceAdded(ByVal sender as Object,ByVal e as
EventArgs)

Private Sub ClearCaches(ByVal OldState as CCOptions,ByVal NewState as
CCOptions)
If ((OldState Xor NewState) And CCOptions.CacheCollections) <> 0
Then
lstCache.Clear()
End If
If ((OldState Xor NewState) And CCOptions.CacheUpdateEvents) <> 0
Then
If Not InstanceAddedEvent is Nothing Then
For Each rb as System.Delegate in
InstanceAddedEvent.GetInvocationList()
RemoveHandler
InstanceAdded,DirectCast(rb,InstanceAddedEventHandler)
Next
End If
End If
End Sub
End Class

As you can see, VB creates a member called <Event Name>Event for each
event you declare. If someone has attached an event handler, then it
will be set to a <Event Name>EventHandler (which derives from
EventHandler). You can then query it's invocation list to get each
subscriber to the event.

HTH,

Damien

 
Reply With Quote
 
S. Senthil Kumar
Guest
Posts: n/a
 
      19th Aug 2005
Just check if the event instance is null, like
delegate void SomeDelegate(object sender, EventArgs e)

class Test
{
event SomeDelegate SomeEvent;

bool AreHandlersRegistered()
{
return SomeEvent != null;
}
}

Regards
Senthil

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking that an event has handlers added to it - using Reflection jehugaleahsa@gmail.com Microsoft C# .NET 8 28th Nov 2007 08:28 AM
how to find event handlers of given event using reflection Grzegorz Danowski Microsoft C# .NET 2 9th Jan 2007 09:27 PM
Checking for handlers for in-built events Larry Lard Microsoft VB .NET 1 13th Jul 2005 03:02 PM
Newbie Question: One event triggers two event handlers MattNC Microsoft C# .NET 2 30th Mar 2005 11:44 PM
Multiple Event Handlers for a Single Event - Newbie Question. Andy Cooper Microsoft VB .NET 8 2nd Jan 2004 04:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:17 PM.