Need to create an event handler that can remove itself when the event occurs

c#, design-patterns, event-handling, wpf

Solution

Try to do something like next:

 RoutedEventHandler handlerLoad = null;
 handlerLoad = delegate
        {
            //Do something
            Window.Close -= handlerLoad;
        };

Window.Close += handlerLoad;

Problem

I'm not really sure how to do this, as I've never had a need for this pattern just yet. I'm looking for the correct pattern on creating an event handler in a separate class that can remove itself when the object that contains the event executes. Basically, I want to create an `EventHandler` that occurs on the WPF `Window.Close` event. And, during the execution of the handler it removes itself from the `Window.Close` event. I hope that's specific enough to go on. Also, is there a specific name for this pattern?

Original source