Search code examples
c#wpfstopwatchdispatchertimer

C# WPF label not updating properly on (idle) GUI


So i'm trying to make a timer system but for whatever reason the label stops updating when you move your mouse over the GUI screen of the application.

Gif showing the issue: https://i.imgur.com/Jvi1AgF.gifv this is what happens on another example i tried.

However on my example the exact opposite happens. It only updates when the GUI is updated. I have no clue how to solve this.

GIF:https://i.imgur.com/KezAVaj.gifv

public partial class MainWindow : Window
{
    Stopwatch _stopwatch;
    DispatcherTimer _timer;
    public MainWindow()
    {
        InitializeComponent();
        _stopwatch = new Stopwatch();
        _timer = new DispatcherTimer();
        _timer.Interval = new TimeSpan(10);
        _timer.Tick += new EventHandler(Tick);

    }

    private void Tick(object sender, EventArgs e)
    {
        LabelTime.Content = _stopwatch.Elapsed.ToString(@"m\:ss\.ff");
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        _stopwatch.Start();
        _timer.Start();
    }
}

Solution

  • Changing

    _timer = new DispatcherTimer();
    

    to

    _timer = new DispatcherTimer(DispatcherPriority.Render);
    

    fixed the issue.