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