In the following code, I create a DispatcherTimer in the class constructor. No one refers to it.
In my understanding, the timer should be returned by the garbage collector some time after leaving the constructor area. But this does not happen! Even after forced garbage collection withGC.Collect()
What happens under the hood?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(100),
IsEnabled = true
}
.Tick += (s, e) =>
{
textBlock1.Text = DateTime.Now.ToString();
};
}
}
source
share