Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Checking for event handlers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Darren - 18 Aug 2005 22:21 GMT
Is there any way to check to see if there are any handlers registered
for a given event?
Damien - 19 Aug 2005 08:30 GMT
> 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
S. Senthil Kumar - 19 Aug 2005 17:14 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.