0

I (thought I) know how to use a DispatcherTimer in a WPF application:

namespace TimerTestbed
{
    public partial class MainWindow : Window
    {
        DispatcherTimer timer = new DispatcherTimer();

        public MainWindow()
        {
            InitializeComponent();

            timer.Interval = TimeSpan.FromMilliseconds(1000);
            timer.Tick += delegate 
            {
                Debug.WriteLine("Tick!");
            };
            timer.Start();
        }
    }
}

This gives me nice regular "Tick!" in the Output window :-)

However, if I use that same code "out in the wild", in it's natural habitat so to speak, I do not get the tick event fired.

public class Patcher : MyObject
    {
        DispatcherTimer timer = new DispatcherTimer();

        public Patcher()
        {
            timer.Interval = TimeSpan.FromMilliseconds(1000);
            timer.Tick += delegate
            {
                Debug.WriteLine("Tick!");
            };
            timer.Start();
        }
    }

I have confirmed, that the Patcher-object is still alive and I have a bunch of other objects that happily fire their events when their respective time has come.

Can anyone point me to how I diagnose this issue? Is there a limitation on the use of a DispatcherTimer that I am unaware of?

Thanks,...

Dirty
  • 11
  • 2
  • Try: Public void Patcher() – Dark Templar Jun 04 '22 at 15:22
  • The "Patcher()" function is the constructor of the object. Afaik, it must be declared w/o a return type. If I add a return type (void) then it is considered just a normal method and throws an error. The idea behind this timer is that it is a sort of 'heartbeat' of the object. – Dirty Jun 12 '22 at 17:57

0 Answers0