0

I have a program that attaches a listener to an object every time the window switches, and then calls a delegate when an event occurs on this window.

The problem is that the code assigns a new delegate every time a window is switched to, meaning one window could have multiple delegates called for one event.

To prevent this I would like to be able to un register a delegate from within the delegate function.

For example:

void OnWindowChange(CustomClass newWindow){
    newWindow.OnEvent += delegate {
        Console.WriteLine("This event happened.");
        newWindow.OnEvent -= *this delegate*;
    };
}

I was considering not using an anonymous function, but the problem is, there are at least 10 events per window object that all need hooked. So anonymous seems to be the best (DRYist) way.

If there is a better way to go about doing this, I am open to it.

Bigbob556677
  • 1,805
  • 1
  • 13
  • 38
  • No you can't do it like that. You'll need to either assign the annonymous function to a variable, use a local function or use a standard non-local function. – Joelius Oct 15 '20 at 16:39
  • Does this answer your question? [One shot events using Lambda in C#](https://stackoverflow.com/questions/2150581/one-shot-events-using-lambda-in-c-sharp) – Joelius Oct 15 '20 at 16:40
  • @Joelius My only concern with that is that each of the many events have different callback parameters, which means I’d have to have many different functions for callback. Is there a way to generalize parameters so that the callbacks don’t care? – Bigbob556677 Oct 15 '20 at 16:41
  • Using reflection for sure but not in a type-safe C#-like way as far as I know. Actually maybe you can do something with variance (see [Variance in Delegates](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/variance-in-delegates)) but that will only work if the amount of parameters stays the same for all callbacks. I unfortunately don't have time to assist you further right now. – Joelius Oct 15 '20 at 16:42
  • @Joelius I’m not using the parameters in the callback, so if there is some sort of scripting-like approach, I don’t mind. – Bigbob556677 Oct 15 '20 at 16:44
  • The question is why you're adding these delegates each time a Window is switched to instead of each time a Window is created and remove the handlers when the Window is destroyed. You could use a *manager* class, pass a new Window to the *manager* which will hook up all the events required. To store the Windows references, you could use a collections that raises ListChanged events, so when the Window is disposed of, you get a notification and you're still in time to remove its existing handlers. – Jimi Oct 15 '20 at 19:31
  • @Jimi I’m not creating a new window. I’m hooking into an actively open window, then listening for events within that window. At any given time, there is only 1 active window object. – Bigbob556677 Oct 15 '20 at 19:33
  • Not important. What counts is that you *register* the existence of that object once and you acknowledge its destruction once (or otherwise remove the handlers you created when the activity related to these objects terminates). Implementation details: can a non-active Window raise those events? If yes, then you just need to let (or give means to) your *manager* identify the active Window and act upon notification only when the notifications come from the *active* object. – Jimi Oct 15 '20 at 19:47

0 Answers0