1

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();
    }
}
DarkEyeDragon
  • 320
  • 5
  • 15
  • `new TimeSpan(10)` creates a TimeSpan of 1 microsecond: https://learn.microsoft.com/en-us/dotnet/api/system.timespan.-ctor?view=netframework-4.7.2#System_TimeSpan__ctor_System_Int64_ – Clemens Jan 31 '19 at 18:01
  • Strange, but this code works fine for me. – Alexander Jan 31 '19 at 18:02
  • @Alexander Seriously, with a 1 microsecond interval? – Clemens Jan 31 '19 at 18:05
  • @Clemens Yes, seriously. – Alexander Jan 31 '19 at 18:09
  • @Alexander But you do realize that it's not exactly very reasonable? I mean updating a Label a million times per second? – Clemens Jan 31 '19 at 18:11
  • @Clemens Of course, you are right but the fact is this is working fine for me. – Alexander Jan 31 '19 at 18:11
  • Have you tried to add `LabelTime.InvalidateVisual();` or/and `LabelTime.UpdateLayout();` after updating content? – Alexander Jan 31 '19 at 18:12
  • 1
    Create the timer with a *reasonable* interval, e.g. `TimeSpan.FromMilliseconds(10)` and optionally increase its [priority](https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatchertimer.-ctor?view=netframework-4.7.2#System_Windows_Threading_DispatcherTimer__ctor_System_Windows_Threading_DispatcherPriority_). – Clemens Jan 31 '19 at 18:14
  • @Clemens i've tried with 1second intervals too or 100/1000 ticks it literally changes nothing. – DarkEyeDragon Jan 31 '19 at 19:18
  • @Alexander Yes i have tried with no effect. – DarkEyeDragon Jan 31 '19 at 19:20
  • changing priority wont change much either as my CPU is under no load whatsoever. – DarkEyeDragon Jan 31 '19 at 19:20
  • @DarkEyeDragon Did you set the priority (e.g. to Normal) or are you just assuming it won't change much? – Clemens Jan 31 '19 at 19:50
  • @Clemens i was assuming and proven wrong. setting the priority to render seemed to have solved the issue. Never had to do this before however. Thanks. – DarkEyeDragon Jan 31 '19 at 20:04

1 Answers1

0

Changing

_timer = new DispatcherTimer();

to

_timer = new DispatcherTimer(DispatcherPriority.Render);

fixed the issue.

Clemens
  • 123,504
  • 12
  • 155
  • 268
DarkEyeDragon
  • 320
  • 5
  • 15