11

Question 1: Hi, I would like to know is there a way by which I can dispose or kill the object of DispatcherTimer and create a new object of same name?

Question 2: Can I access the DispatcherTimer object in some other class if it is set to Public?

Stas Ivanov
  • 1,173
  • 1
  • 14
  • 24
ahmad05
  • 438
  • 2
  • 6
  • 23

2 Answers2

22
  1. You cannot dispose DispatcherTimer object. It doesn't implement IDisposable interface. You cannot explicit kill (free, destroy) objects in managed world. If you don't need the timer object any more, disable it and set reference to it to null. It will be collected later by GC. You can disable or stop the timer by setting IsEnabled = false or call timer.Stop(). The effect is the same.
  2. Yes. I suppose you have public property like this:

    public DispatcherTimer MyTimer { get; private set; }

Jonas G. Drange
  • 8,749
  • 2
  • 27
  • 38
Lubo
  • 754
  • 6
  • 8
  • yes that stopping the timer works fine but was eager to know if i could dispose it some how.... i have worked my way around – ahmad05 Nov 28 '12 at 13:17
  • 2
    IsEnable cannot be set. timer.stop does not dispose tick event. i still get duplicate tick event even after assigning DispatcherTimer to a new DispatcherTimer. – Syaiful Nizam Yahya Mar 03 '14 at 11:32
  • The question is tagged with WPF. That's why I refer in my answer to System.Windows.Threading.DispatcherTimer class from WindowsBase assembly not to Windows.UI.Xaml.DispatcherTimer from Windows Runtime API. – Lubo Aug 06 '14 at 09:32
  • 1
    @SyaifulNizamYahya this is why I use `myTimer1.Tick -= OnTimerTick_Move; myTimer1.Tick += new EventHandler(OnTimerTick_Move);` when the timer will be started multiple times in order to deattach the event handler or you could do it when you call `myTimer1.Stop();` – vinsa Mar 20 '18 at 16:57
  • 1
    @vinsa unwiring from the handler is smart. Will your code unwire the last time though? – gusmally supports Monica Jan 11 '20 at 01:33
4

Adding to a correct answer from Lubo (and bringing up this topic from comments under it): even though you cannot dispose DispatcherTimer (most probably, because it's wired up to unmanaged part of the WPF / UWP Dispatcher itself which lives as long as the app itself), you still should unsubscribe from its events.

Say, if you had some method (StartRefreshTimer) where you initialized your DispatcherTimer and started listening to its Tick event:

private DispatcherTimer _refreshTimer = new DispatcherTimer() { Interval = TimeSpan.FromMinutes(1) };

private void StartRefreshTimer()
{
    if (_refreshTimer != null)
    {
        _refreshTimer.Tick += OnTick; // subscribe to timer's ticks
        _refreshTimer.Start(); // start timer
    }
}

private void OnTick(object sender, object args)
{
    // your custom OnTick logic
}

Then you should have a method which stops the timer and unsubscribes from its events:

private void StopRefreshTimer()
{
    if (_refreshTimer != null)
    {
        _refreshTimer.Stop(); // stop timer
        _refreshTimer.Tick -= OnTick; // unsubscribe from timer's ticks
    }
}

You should make sure you call this "tear down" method when your class goes out of scope (for example, when your WPF / UWP control or ViewModel is unloaded). If you don't unsubscribe from timer events you could end up with memory leaks caused by references from outer scope to your timer hosting class.

Stas Ivanov
  • 1,173
  • 1
  • 14
  • 24