Page 1 of 1

The chart shows an error when adding lines

Posted: Thu Sep 17, 2020 3:01 am
by 15687497
In my program, I need to dynamically add points to the line.
When I click the button, a line is created, and the method is called regularly to add points.
The line will be recreated when the button is clicked again.
The core code is as follows.

First add a line to the chart through the following code:

Code: Select all

private void btn_Click(object sender, EventArgs e)
{
    tChart1.Series.Clear();
    line = new Line(tChart1.Chart)
    {
        Title = "line1",
        VertAxis = VerticalAxis.Both,
        HorizAxis = HorizontalAxis.Both,
        Color = colorBtn.BackColor
    };
}
Then call the following method to add points:

Code: Select all

public void AddPoint(double x, double y)
{
    line.Add(x, y);
}
Occasionally the phenomenon shown in the figure below will appear. How can I prevent this from happening?

Re: The chart shows an error when adding lines

Posted: Thu Sep 17, 2020 7:55 am
by Christopher
Hello,

which version of TeeChart are you using?

I've prepared a TeeChart example using your code and the latest TeeChart version on NuGet which you can download from here:
http://steema.com/files/public/support/ ... esTEST.zip

As you can see, I'm adding in ten points a second using a timer, and the problem you indicate doesn't seem to occur. Does this example work as expected at your end? Could you please modify the example so I can reproduce your problem at my end?

Re: The chart shows an error when adding lines

Posted: Fri Sep 18, 2020 3:50 am
by 15687497
Thanks for your answer,I am also using the latest version on NuGet.
In my program, the line is created every time the button is clicked. Like the following code:

Code: Select all

        private void button1_Click(object sender, EventArgs e)
        {
            //if(timer1.Enabled)
            //{
            //    timer1.Enabled = false;
            //    ChangeButtonTitle(true);
            //}
            //else
            //{
            //    timer1.Enabled = true;
            //    ChangeButtonTitle(false);
            //}
            AddSeries();
            timer1.Enabled = true;
        }
Of course the actual code is more complicated.
This situation does not happen every time, but only in a very small number of times.

Re: The chart shows an error when adding lines

Posted: Fri Sep 18, 2020 7:54 am
by Christopher
Hello!

A couple of things:
1) there's a new version of TeeChart on NuGet, v.4.2020.9.16 - does the problem still occur with this version?
2) using the project I sent you, I changed the button click code to this:

Code: Select all

        private void button1_Click(object sender, EventArgs e)
        {
            if(timer1.Enabled)
            {
                timer1.Enabled = false;
                ChangeButtonTitle(true);
            }
            else
            {
                timer1.Enabled = true;
                ChangeButtonTitle(false);
                AddSeries();
            }
        }
I then clicked the button 200 times in a row to try and reproduce the problem, but received no error. How many times do you have to click the button before the error occurs using this code?

Re: The chart shows an error when adding lines

Posted: Wed Sep 30, 2020 8:39 am
by 15687497
It's not an easy Recurring problem.it did not happen in subsequent runs.May not be the problem of adding lines.
But this is a hidden danger in my code.Do you have any other suggestions for this situation?

Re: The chart shows an error when adding lines

Posted: Thu Oct 01, 2020 6:25 am
by Christopher
Elite wrote:
Wed Sep 30, 2020 8:39 am
It's not an easy Recurring problem.it did not happen in subsequent runs.May not be the problem of adding lines.
But this is a hidden danger in my code.Do you have any other suggestions for this situation?
The 'hidden danger' may be multithreading - GDI+ is not thread-safe, which is why it is not recommended in future development as you can see at the bottom of this page. Other than this, and in the absence of being able to reproduce your issue here, I'm afraid I can't think of anything else.