none
How can i check the event is already have subscription in the current class. RRS feed

  • Question

  • I am having a event inside a singleton class.This event is subscribed in multiple classes.

    After every workflow i am calling a cleanup method inside that i am unsubscribing the event.

    But my question here it is how can i check before subscribing wheather this has any subscription in the current class?

            StatusMonitor.Instance.StateChangeNotification += InstanceOnStateChangeNotification;


            private void InstanceOnStateChangeNotification(object sender, MotionStateChangeNotification e)
            {


            }



    • Edited by La07K Friday, October 12, 2018 12:53 PM
    Wednesday, October 10, 2018 11:44 AM

Answers

  • There is no built in way to query or access the list that is being maintained. There are a couple of options people have used in the past.

    1) You can override the add/remove methods for an event. If you do that then you have complete control over events because you're responsible for tracking them. This is rarely done but would be one way. You could then have a public member that specifies whether the given instance/method combination is already in your list.

    2) Another option would be to fall back to the MulticastDelegate that events are based off of. It takes a little code but you can get the list of handlers and enumerate them. Google for some sample code.

    I question why you're doing this though. If you only want once instance to ever handle the event then don't make the handler an instance method, make it static. Hook up the handler in the static ctor of the type itself. Otherwise each instance should be responsible for handling the event. If that doesn't make sense for your scenario then you may have a design problem.

    If you want to just ensure you don't have dups then first remove the handler from the event and then add it back. 


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 10, 2018 2:13 PM
    Moderator

All replies

  • There is no built in way to query or access the list that is being maintained. There are a couple of options people have used in the past.

    1) You can override the add/remove methods for an event. If you do that then you have complete control over events because you're responsible for tracking them. This is rarely done but would be one way. You could then have a public member that specifies whether the given instance/method combination is already in your list.

    2) Another option would be to fall back to the MulticastDelegate that events are based off of. It takes a little code but you can get the list of handlers and enumerate them. Google for some sample code.

    I question why you're doing this though. If you only want once instance to ever handle the event then don't make the handler an instance method, make it static. Hook up the handler in the static ctor of the type itself. Otherwise each instance should be responsible for handling the event. If that doesn't make sense for your scenario then you may have a design problem.

    If you want to just ensure you don't have dups then first remove the handler from the event and then add it back. 


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 10, 2018 2:13 PM
    Moderator
  • Thank You



    • Edited by La07K Friday, October 12, 2018 12:53 PM
    Thursday, October 11, 2018 11:20 AM