We've learned the hard way that if Stop() is not called on DispatcherTimer instances, once they are started, they continue to live forever, even if nothing references the instance anymore.
After looking at the implementation of DispatcherTimer, we noticed that when calling Start(), a reference to the DispatcherTimer is added to the Dispatcher.CurrentDispatcher (which is static) and the reference is only removed once the Stop() method is called.
Now knowing that Stop() must always be called in order to avoid memory leaks, shouldn't the class implement IDisposable and make sure to call Stop() in the Dispose()? This would indicate the consumer of the class that it needs to be disposed for resources to be released.
Same way System.Threading.Timer implements IDisposable, I think DispatcherTimer should as well.
We've learned the hard way that if
Stop()is not called onDispatcherTimerinstances, once they are started, they continue to live forever, even if nothing references the instance anymore.After looking at the implementation of
DispatcherTimer, we noticed that when callingStart(), a reference to theDispatcherTimeris added to theDispatcher.CurrentDispatcher(which is static) and the reference is only removed once theStop()method is called.Now knowing that
Stop()must always be called in order to avoid memory leaks, shouldn't the class implementIDisposableand make sure to callStop()in theDispose()? This would indicate the consumer of the class that it needs to be disposed for resources to be released.Same way
System.Threading.TimerimplementsIDisposable, I thinkDispatcherTimershould as well.