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

  • Thread starter Thread starter James Hancock
  • Start date Start date
J

James Hancock

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
 
James said:
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.

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;
}
 
Thanks! That solves my problem!

Larry Lard said:
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;
}
 
Back
Top