dotnet / dotnet/wpf

DispatcherTimer should implement the IDisposable interface

Open
#8,839 8 comments 0 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.