0

Could you please tell me why the chart is updated after the last iteration (i = 99)?

    private void music_play_button_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            (hr_plot.Model.Series[0] as LineSeries).Points[0] = new DataPoint(i, HRConstants.HRMin);
            (hr_plot.Model.Series[0] as LineSeries).Points[1] = new DataPoint(i, HRConstants.HRMax);
            System.Threading.Thread.Sleep(10);
            hr_plot.Model.InvalidatePlot(true);
        };
    }

The chart should change after every iteration, not after the whole loop. What is the proper solution?

Michał Dobi Dobrzański
  • 1,449
  • 1
  • 20
  • 19

1 Answers1

1

The Buttoneventhandler blocks the UI-Thread while computing the new data points. As consequence, the chart only chances once the eventhandler is finished

Solution: You could create a new dispatchertimer in your eventhandler and caluclate a single iteration on every timerTick

Philip W
  • 781
  • 3
  • 7
  • Thank you, it helped me clarify some things. However, now I get sloppy chart updating. As I have read, it might be connected with not constant rate of Dispatcher' event firing. Any clues how to avoid this? In my code the PLAY button does: h_DispatcherTimer.Start(). And at every Dispatcher Timer tick (every 40ms = 25FPS) it does: // increase the global counter, check if the global variable exceeds the max of the counter and update the chart with the following method: hr_plot.Model.InvalidatePlot(true); – Michał Dobi Dobrzański Jul 06 '15 at 15:50