0

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

user2980207
  • 293
  • 1
  • 11
  • 10 ms is just not a sensible interval for a DispatcherTimer. – Clemens Apr 14 '19 at 21:17
  • What should be used alternatively for updating a label every 10ms and having the change appear on the ui? – user2980207 Apr 14 '19 at 21:20
  • @Clemens also the "duplicate" you linked is not exactly related to the question i'm asking. Yes he is using a DispatcherTimer, no he is not updating the ui thread, no he did not receive an actual solution. – user2980207 Apr 14 '19 at 21:37
  • 1
    It makes no sense to update a label 100 times per seconds: https://en.wikipedia.org/wiki/Frame_rate. WPF runs with a maximum rendering frame rate of ~60 fps. – Clemens Apr 14 '19 at 21:54

0 Answers0