Alex Rivera | Logout

How to determine if an event is already subscribed

Asked 2010-04-23T08:45:52.783
79

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?

Edit
Report

1 Answer

0

Can't you just remember whether you already subscribed? That approach worked fine for me so far. Even if you have a lot of events or objects, you may still want to just remember that (in a dictionary, for example).

On the other hand, visibility change was, at least for me, not a good point to subscribe/unsubscribe. I typically rather go with construction / Disposed, which are more clear than each time visibility changes.

answered 2010-04-23T08:56:19.427

Your Answer