Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if an event is already subscribed

Tags:

c#

.net

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?

like image 455
Ram Avatar asked Apr 23 '10 08:04

Ram


People also ask

How to check if event already subscribed c#?

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.

Can we have two subscribers for a single event?

An event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers. Events that have no subscribers are never raised.

How do you know if an event handler is null?

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.


1 Answers

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; } 
like image 99
Hans Passant Avatar answered Sep 21 '22 06:09

Hans Passant