In my .NET application I am subscribing to events from another class. The subscription is conditional. I am subscribing to events when the control is visible and de-subscribing it when it become invisible. However, in some conditions I do not want to de-subscribe the event even if the control is not visible as I want the result of an operation which is happening on a background thread.
Is there a way through which I can determine if a class has already subscribed to that event?
I know we can do it in the class which will raise that event by checking the event for null
, but how do I do it in a class which will subscribe to that event?
Assuming that you have no access to the innards of the class declaring the event, you have no way to do it directly. Events only expose operators += and -= , nothing else. You will need a flag or some other mechanism in your subscribing class to know whether you are already subscribed or not.
An event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers. Events that have no subscribers are never raised.
Event handler can not be null or not, because event handler is a piece of code, a method, anonymous or not. Only the instance of event handle can be null or not. That's it. Checking up if the event instance is null is impossible outside the class declaring the event.
The event
keyword was explicitly invented to prevent you from doing what you want to do. It restricts access to the underlying delegate
object so nobody can directly mess with the events handler subscriptions that it stores. Events are accessors for a delegate, just like a property is an accessor for a field. A property only permits get and set, an event only permits add and remove.
This keeps your code safe, other code can only remove an event handler if it knows the event handler method and the target object. The C# language puts an extra layer of security in place by not allowing you to name the target object.
And WinForms puts an extra layer of security in place so it becomes difficult even if you use Reflection. It stores delegate
instances in an EventHandlerList
with a secret "cookie" as the key, you'd have to know the cookie to dig the object out of the list.
Well, don't go there. It is trivial to solve your problem with a bit of code on your end:
private bool mSubscribed; private void Subscribe(bool enabled) { if (!enabled) textBox1.VisibleChanged -= textBox1_VisibleChanged; else if (!mSubscribed) textBox1.VisibleChanged += textBox1_VisibleChanged; mSubscribed = enabled; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With