Matt raises the question of how you attach an anonymous method so that there is no easy way to separate it. Here is a generic template that you can use so you can disconnect if necessary.
private void MyFunction() { DispatcherTimer timer = new DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, 1); EventHandler eh = null; eh = (object sender, object e) => { timer.Tick -= eh; timer.Stop();
However, in this particular case, there is nothing wrong with the way your source code works, as the timer becomes collectable as soon as it stops.
source share