Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Unsubscribing all event listeners

Unsubscribing all event listeners

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sjembek
    wrote on last edited by
    #1

    Is there any way to unsubscribe all event listeners for a certain event?

    public delegate void myHandler(object Sender);
    public event myHandler somethingChanged;

    private void onSomethingChanged()
    {
    if(somethingChanged != null) somethingChanged(this);
    }

    What I would like to do is something like somethingChanged = null. Why isn't this allowed if I can actually check somethingChanged for null? somethingChanged -= someFunction is not an option, what I want is somethingChanged -= allListeners. Can anyone help me out here?

    B 1 Reply Last reply
    0
    • S sjembek

      Is there any way to unsubscribe all event listeners for a certain event?

      public delegate void myHandler(object Sender);
      public event myHandler somethingChanged;

      private void onSomethingChanged()
      {
      if(somethingChanged != null) somethingChanged(this);
      }

      What I would like to do is something like somethingChanged = null. Why isn't this allowed if I can actually check somethingChanged for null? somethingChanged -= someFunction is not an option, what I want is somethingChanged -= allListeners. Can anyone help me out here?

      B Offline
      B Offline
      beatles1692
      wrote on last edited by
      #2

      Hi, I don't know how to exactly do this but there should be a way. I want to ask why do you want to do this ? because as far as I know your host doesn't know anything about its listeners.It's the responsibility of a listener to register or unregister itself. Anyway how about having a flag variable that if set,it will stop the service. For example: public viod Stop() { stopped=true; } private void OnSomethingChanged() { if (stopped) return; }

      S 1 Reply Last reply
      0
      • B beatles1692

        Hi, I don't know how to exactly do this but there should be a way. I want to ask why do you want to do this ? because as far as I know your host doesn't know anything about its listeners.It's the responsibility of a listener to register or unregister itself. Anyway how about having a flag variable that if set,it will stop the service. For example: public viod Stop() { stopped=true; } private void OnSomethingChanged() { if (stopped) return; }

        S Offline
        S Offline
        sjembek
        wrote on last edited by
        #3

        Thx for the answer, wasn't expecting anyone to reply anymore. I've thought about that solution too, but I think there should be a more elegant way to fix the issue. The reason I want to do this: the class concerned here contains some data that's shown and can be modified in dozens of places in my application. I've created a sort of "undo" function that saves a copy of the object to memory so that the user can "rollback" on his actions. These copies should contain only data, and shoudn't even try to fire any events. While debugging I found out that some references were left to my cloned objects, meaning the data in these clones is getting modified just as my "foreground" data. What's more, the number of cloned objects in memory could eventually be many thousands, so the performance hit for even for simply checking a flag each time isn't something I want. Since manually disconnecting my eventlisteners is so sensitive to my own errors (code intensive) I'd like to be sure all eventlisteners get disconnected from the event. I'd expect the event itself to contain a list of references to all listeners. As I said before: How else could you check an event for null. Suggestions are still welcome of course :)

        B 1 Reply Last reply
        0
        • S sjembek

          Thx for the answer, wasn't expecting anyone to reply anymore. I've thought about that solution too, but I think there should be a more elegant way to fix the issue. The reason I want to do this: the class concerned here contains some data that's shown and can be modified in dozens of places in my application. I've created a sort of "undo" function that saves a copy of the object to memory so that the user can "rollback" on his actions. These copies should contain only data, and shoudn't even try to fire any events. While debugging I found out that some references were left to my cloned objects, meaning the data in these clones is getting modified just as my "foreground" data. What's more, the number of cloned objects in memory could eventually be many thousands, so the performance hit for even for simply checking a flag each time isn't something I want. Since manually disconnecting my eventlisteners is so sensitive to my own errors (code intensive) I'd like to be sure all eventlisteners get disconnected from the event. I'd expect the event itself to contain a list of references to all listeners. As I said before: How else could you check an event for null. Suggestions are still welcome of course :)

          B Offline
          B Offline
          beatles1692
          wrote on last edited by
          #4

          Hi This case can be an example of memento design pattern.Using this pattern you can save state of your objects in another object(a memento) and then you can put you object back to its initial state. for example: public class Test { private int x; public EventHandler XChanged; public int X { get{return x;} set { this.x=value; if(this.XChanged!=null) XChanged(this,EventArgs.Empty); } public TestMemento GetMemento() { TestMemento memento=new TestMemento(); memento.AddField("x",this.x"); } public void SetMemento(TestMemento memento) { this.x=(int)memento.GetField("x"); } } and here is TestMemento public class TestMemento { public void AddField(string key,object value) { this.fieldsMap.Add(key,value); //fieldsMap is a hashtable } public object GetField(string key) { return this.fieldsMap[key]; } } this way you can save an object state and load it again. Take a look at serialization patterns in C# and .Net framework. Regards

          S 1 Reply Last reply
          0
          • B beatles1692

            Hi This case can be an example of memento design pattern.Using this pattern you can save state of your objects in another object(a memento) and then you can put you object back to its initial state. for example: public class Test { private int x; public EventHandler XChanged; public int X { get{return x;} set { this.x=value; if(this.XChanged!=null) XChanged(this,EventArgs.Empty); } public TestMemento GetMemento() { TestMemento memento=new TestMemento(); memento.AddField("x",this.x"); } public void SetMemento(TestMemento memento) { this.x=(int)memento.GetField("x"); } } and here is TestMemento public class TestMemento { public void AddField(string key,object value) { this.fieldsMap.Add(key,value); //fieldsMap is a hashtable } public object GetField(string key) { return this.fieldsMap[key]; } } this way you can save an object state and load it again. Take a look at serialization patterns in C# and .Net framework. Regards

            S Offline
            S Offline
            sjembek
            wrote on last edited by
            #5

            Hi, Thanks for the suggestion, someone has offered me the memento pattern before, and next time I'm implementing a feature like this I'll be sure to use it. Unfortunately I found out a bit late about this in order to use it for the current project: Should I want to convert my project to use this pattern would mean I'd have to change about 1500 base datatypes to start using it, I'm afraid I don't have the time or spirit to do so. For now I found an ugly hack around the problem: I serialize the class and store it to a memorystream, then inmedialtely deserialize it and add it to my Collection of "undo" objects. Since the event is marked as non-serializable it gets initialized to null as soon as I deserialize it. It's a bit ugly and I still don't see why I can't just set event myEvent = null . But this performs quite well actually. Cheers and thanks for the help! -- modified at 10:10 Friday 25th August, 2006

            B 1 Reply Last reply
            0
            • S sjembek

              Hi, Thanks for the suggestion, someone has offered me the memento pattern before, and next time I'm implementing a feature like this I'll be sure to use it. Unfortunately I found out a bit late about this in order to use it for the current project: Should I want to convert my project to use this pattern would mean I'd have to change about 1500 base datatypes to start using it, I'm afraid I don't have the time or spirit to do so. For now I found an ugly hack around the problem: I serialize the class and store it to a memorystream, then inmedialtely deserialize it and add it to my Collection of "undo" objects. Since the event is marked as non-serializable it gets initialized to null as soon as I deserialize it. It's a bit ugly and I still don't see why I can't just set event myEvent = null . But this performs quite well actually. Cheers and thanks for the help! -- modified at 10:10 Friday 25th August, 2006

              B Offline
              B Offline
              beatles1692
              wrote on last edited by
              #6

              Hi Well serialization can be a good replacement for memento design pattern(except that you have to violate encapsulation somehow ,if you are using attributes) I'm happy that you find a solution. Regards

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups