Will it collect trash?

Can I use Lambda as an EventHandler? It seems to me that the handler was correctly deleted and that the garbage collector should clean it. However, I didn’t see anyone else do it this way, so I thought it was better to double check.

At the same time, what is the best tool (preferably free) to use to check if it is garbage collection?

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(10);

EventHandler callback = null;
callback = (s, e) =>
    {
        timer.Stop();
        timer.Tick -= callback;
    };

timer.Tick += callback;
timer.Start();
+5
source share
2 answers

Yes, the callback will have the right to garbage collection after the callback is made once. This is a smart way to subscribe to an event, to execute the handler only once.

, - , , callback timer, , ... ( , , ), .

+3

A DispatcherTimer Dispatcher, List<DispatcherTimer> , . () , , , . , .

, . - . OOM, , , . , , 15 - . , .

+4

All Articles