0

I developed a simple windows form application showing a realtime updated chart:

Screenshot

In the following code:

        private void Start_Click(object sender, EventArgs e)
        {
            cpuThread = new Thread(new ThreadStart(this.ThreadUpdateChart));
            cpuThread.IsBackground = true;
            cpuThread.Start();
        }

        private void ThreadUpdateChart()
        {
            if (chart1.IsHandleCreated)
            {
                this.Invoke((MethodInvoker) delegate { UpdateChart();});
            }
            else
            {
                //......
            }

            Thread.Sleep(1000);
        }

        private void UpdateChart()
        {
            for (int i = 0; i < sampleSize; i++)
            {
                chart1.Series["Signal"].Points.AddXY(i, 100 * var.NextDouble());
                chart1.Update();
                //Thread.Sleep(500);
            }
        }

My problem is that when I press the start button the chart is update correctly, but this freeze my GUI till the end of the updated, I would like to update the chart without freezing other GUI functions, is it possible? Any solution?

Paul Lake
  • 11
  • 1
  • Did you look at this? https://stackoverflow.com/questions/49859237/how-do-i-stop-my-ui-from-freezing – Svein Terje Gaup Dec 06 '21 at 11:45
  • if possible only do the chart1.Update once you do it now per every single value. And see if setting the points really needs to be synchronized. Maybe only calling chart1.Update() needs to be. – Ralf Dec 06 '21 at 12:04
  • Background workers are mainly used for such purposes, i.e. to safely access the GUI from another thread. You can find some good examples for the same on the internet, but I'll leave a link-https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=net-6.0 – D J Dec 06 '21 at 12:07
  • What is the value of `sampleSize`? What's the reason for calling `chart1.Update();` on every iteration? – Theodor Zoulias Dec 06 '21 at 12:30
  • 1
    Already tried with backgroudworker, nothing chage at all. The chart is live updated as I want but during chart update I cannot use other buttons/functions in the form – Paul Lake Dec 06 '21 at 15:12

1 Answers1

1

To start of, the threading does nothing useful in your example, it just runs some code on a background thread, that immediately asks the main thread to update the chart. So get rid of it unless you are doing something computationally expensive not shown in the example.

You should also get better performance by doing all changes to your chart at once, avoiding expensive things like rendering until all changes have been made. So I would try something like this:

private void UpdateChart()
        {
            var series  = chart1.Series["Signal"];
            for (int i = 0; i < sampleSize; i++)
            {
                series.Points.AddXY(i, 100 * var.NextDouble());
            }
            chart1.Update();
        }

If this does not help I would recommend doing some profiling to find out what is actually blocking the UI thread.

JonasH
  • 28,608
  • 2
  • 10
  • 23