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 / April 2006

Tip: Looking for answers? Try searching our database.

How to Detect if you're still subscribed to an event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James Hancock - 20 Apr 2006 19:05 GMT
Ladies and Gents,

I have an event that I raise.  When that event is raised I raise it like
this:

if (NewAlert == null) return;

AlertEventArgs aea = new AlertEventArgs(EmployeeID, AlertID);

EventHandler<AlertEventArgs> eh = null;

foreach (Delegate del in NewAlert.GetInvocationList()) {

try {

eh = (EventHandler<AlertEventArgs>)del;

eh(this, aea);

} catch (Exception ex) {

Console.WriteLine(ex.Message);

NewAlert -= eh;

}

}

The benefit is that I can remove any event handlers that aren't there
anymore and it works like a dispatch so if the subscription is dead for
whateve reason it's removed. Good stuff, I like it, makes everything faster
because it's not trying dead events over and over again.

The problem is that on the other side of the event when it's subscribed to,
I can't figure out how to periodically check to see if I'm still subscribed
to the event in the object.  I need to be able to do my += stuff and then
later on a timer say "Is that += still hooked up?" and get a yes or no so
that I can resubscribe just in case it was something bogus that happened
that caused the event to be unsubscribed (like an unintended bug that causes
the above code to fall into the catch)

Does anyone know how to test if an event handler is still hooked up? I can't
find any documentation on the subject.

Thanks,

James Hancock
Larry Lard - 21 Apr 2006 10:56 GMT
> Ladies and Gents,
>
[quoted text clipped - 32 lines]
> Does anyone know how to test if an event handler is still hooked up? I can't
> find any documentation on the subject.

I suspect that there isn't any automatic way for doing this - because
the information on how the event's invocation list is stored is a
private implementation detail of the class containing the event. As far
as clients are concerned, all they are allowed to do with
<object>.NewAlert is:

- add delegates
- remove delegates

To quote the spec:

Since += and -= are the only operations that are permitted on an event
outside the type that declares the event, external code can add and
remove handlers for an event, but cannot in any other way obtain or
modify the underlying list of event handlers.

So I think you'd have to add a method to the class which enables
clients to supply a delegate and ask, is this delegate hooked up to
your NewAlert event? Then internally to the client, it would be OK to
go through the the GetInvocationList and check:

       public bool IsAttached(EventHandler eh)
       {
           if (TheEvent != null)
           {
               foreach (EventHandler itereh in
TheEvent.GetInvocationList())
               {
                   if (itereh == eh)
                       return true;
               }
           }

           return false;
       }

Signature

Larry Lard
Replies to group please

James Hancock - 23 Apr 2006 16:42 GMT
Thanks! That solves my problem!

>> Ladies and Gents,
>>
[quoted text clipped - 73 lines]
>            return false;
>        }

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.