DispatcherTimer not firing in WPF Application

Jack picture Jack · Dec 7, 2011 · Viewed 8.3k times · Source

I am trying to understand why the DispatcherTimer contained within SingletonWithTimer is not firing in the following WPF application. I've been researching this for a couple of days and cannot seem to get to the bottom it. This application is the reduced essential parts of an existing application that I'm trying to fix. The Startup object of this project is WPFApplication5TimerTest.Program.

The output in the console lists as follows, the problem is evident because the word "TimerTick" is not shown in the output:

Timer is initialized
'WpfApplication5TimerTest.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Sample thread
Sample thread
Sample thread
Sample thread
Sample thread
Sample thread
The thread '<No Name>' (0x10b0) has exited with code 0 (0x0).
Sample thread exiting!

This is Program.cs:

using System;

namespace WpfApplication5TimerTest
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            AppObject = new App();
            AppObject.Run();
        }

        public static App AppObject
        {
            get;
            private set;
        }
    }
}

This is App.xaml.cs:

using System;
using System.Threading;
using System.Windows;

namespace WpfApplication5TimerTest
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            var sampleThread = new Thread(new ThreadStart(SampleThreadEntryPoint));            
            sampleThread.Start();
            new MainWindow().Show();
        }

        private void SampleThreadEntryPoint()
        {
            SingletonWithTimer.Initialize();
            while (!_shutdownEvent.WaitOne(1000))
                Console.WriteLine("Sample thread");
            Console.WriteLine("Sample thread exiting!");
        }

        protected override void OnExit(ExitEventArgs e)
        {
            _shutdownEvent.Set();
        }

        private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);        
    }
}

This is MainWindow.xaml.cs:

using System;
using System.Windows;

namespace WpfApplication5TimerTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Program.AppObject.Shutdown();
        }
    }
}

This is SingletonWithTimer.cs:

using System;
using System.Windows.Threading;

namespace WpfApplication5TimerTest
{
    public class SingletonWithTimer
    {
        private static SingletonWithTimer Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new SingletonWithTimer();
                }
                return _instance;
            }
        }

        public static void Initialize()
        {
            SingletonWithTimer.Instance._timer = new DispatcherTimer();
            SingletonWithTimer.Instance._timer.Interval = TimeSpan.FromSeconds(2);
            SingletonWithTimer.Instance._timer.Tick += new EventHandler(SingletonWithTimer.Instance.OnTimerTick);
            SingletonWithTimer.Instance._timer.Start();
            Console.WriteLine("Timer is initialized");
        }

        private void OnTimerTick(object sender, EventArgs e)
        {
            Console.WriteLine("TimerTick");
        }

        private static SingletonWithTimer _instance;
        private DispatcherTimer _timer = null;
    }
}

Answer

Kent Boogaart picture Kent Boogaart · Dec 8, 2011

It's because you've created the DispatcherTimer on another (non-UI) thread. Hence, it will be tied to the Dispatcher on that thread, not the one on the UI thread. Since nothing is running the Dispatcher on that thread, it will never fire.

Either create the DispatcherTimer on the UI thread, or use a constructor overload that allows you to pass in a specific Dispatcher to use.