Is it possible to stop multiple subscribers from subscribing to an event?
I have created a quick example snippet to give my question some context but unfortunately I can't test it right now because I'm not at my VS machine.
The goal is to:
Is this possible?
public delegate List<IBaseWindow> GetWindowListDelegate();
public static event GetWindowListDelegate GetWindowListEvent;
public List<IBaseWindow> GetWindowList() {
if (GetWindowListEvent == null) {
return new List<IBaseWindow>();
}
return GetWindowListEvent();
}
Note: I'm using .NET 3.5 sp1.
You could use event accessors to accomplish this. Something like the following:
private EventHandler _h;
public event EventHandler H {
add {
if (...) { // Your conditions here.
// Warning (as per comments): clients may not
// expect problems to occur when adding listeners!
_h += value;
}
}
remove {
_h -= value;
}
}
As Andrew points out, you don't really need events to accomplish this. Is there some particular reason you need them?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With