Edit: This is not a duplicate of dispatcherTimer doesn't work accurately . That question is not pertinent to updating the ui thread but just a dispatcher timer in itself.
I'm trying to make a C# WPF program that will just increment some variable and update a label every X miliseconds.
I created a C# WPF window with a label named Label_test and set up a dispatcher timer to execute every 10 miliseconds which will increment an integer and update a label.
using System;
using System.Windows;
using System.Windows.Threading;
namespace WPF_Test
{
public partial class MainWindow : Window
{
DispatcherTimer dt = null;
public MainWindow()
{
InitializeComponent();
dt = new DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 10);
dt.Tick += Dt_Tick;
dt.Start();
}
int value = 0;
private void Dt_Tick(object sender, EventArgs e)
{
value += 1;
Label_test.Content = "Value: " + value.ToString();
}
}
}
I would expect the label to be updated approximately every 10 miliseconds and the ui element to be refreshed. Instead I get this weird random interval behavior as seen in this video where it appears to sometimes just block and never seems to go nearly as fast as it should. https://drive.google.com/file/d/1ONg9i_F_3f-Oy_fnDHcaD9-GtaReL-78/view