Unsubscribing all event listeners
-
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 checksomethingChanged
for null?somethingChanged -= someFunction
is not an option, what I want issomethingChanged -= allListeners
. Can anyone help me out here? -
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 checksomethingChanged
for null?somethingChanged -= someFunction
is not an option, what I want issomethingChanged -= allListeners
. Can anyone help me out here?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; }
-
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; }
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 :) -
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 :)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 TestMementopublic 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 -
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 TestMementopublic 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. RegardsHi, 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 setevent myEvent = null
. But this performs quite well actually. Cheers and thanks for the help! -- modified at 10:10 Friday 25th August, 2006 -
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 setevent myEvent = null
. But this performs quite well actually. Cheers and thanks for the help! -- modified at 10:10 Friday 25th August, 2006Hi 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