0

I met this problem while I try to use a while and keep refresh the chart. At first, I use function: AddY() and function: removeAt() to update the newest data, such as Example 1.

I also try to use an array to save the data and left-shift the data by myself, and add the newest data. After the refresh the array I use function: DataBindY() to draw the chart. Please reference the Example 2.

There're no error during the build and the function is normal at first, wait a random time, the chart will turn into a red cross and jump out the error message which say "Collection was modified; enumeration operation may not execute."

There're two weird things the build is success, it means the code is correct, right? Second is that "random time", every time I try the survive time is different. It really doesn't make sense. I have search this error message on the net, but I couldn't find the familiar situation.

Does anyone have met the same situation could help me with this?

Thx.

BTW, my window system is Win7 and version of C# is 2010.

Example 1

bool fg_remove_chart_point = false;
uint counter = 0;
while (true)
{
    Thread.sleep(1000);
    if (counter > 100)
        fg_remove_chart_point = true;

    if (fg_remove_chart_point)
    {
        chart_1.ChartAreas[0].AxisX.Minimum = 0;
        chart_1.ChartAreas[0].AxisX.Maximum = 100 + 2;

        chart_1.Series[0].Points.RemoveAt(0); //Delete
        chart_1.Update();
    }
    chart_1.Series[0].Points.AddY(50); //Draw chart
    chart_1.Update();   
    counter++;
}

Example 2

double[] temperature = { };
bool fg_remove_chart_point = false;
uint counter = 0;
while (true)
{
    Thread.sleep(1000);

    if (counter > 100)
        fg_remove_chart_point = true;

    if (fg_remove_chart_point)
    {
        chart_1.ChartAreas[0].AxisX.Minimum = 0;
        chart_1.ChartAreas[0].AxisX.Maximum = 100 + 2;

        //Update the array
        for (int x = 0; x < (100 - 1); x++)
            temperature[x] = temperature[x + 1];

        temperature[100 - 1] = Convert.ToDouble(50);
    }
    else
    {
        Array.Resize<double>(ref temperature, Convert.ToInt16(counter));
        temperature[counter - 1] = Convert.ToDouble(50);
    }

    chart_1.Series[0].Points.Clear();
    chart_1.Series[0].Points.DataBindY(temperature); //Use array to draw chart
    chart_1.Update();

    counter++;
}
James
  • 1
  • 3
    looks like a thread-safety issue. Are this collection modified and viewed from different threads? – vasily.sib Jun 14 '18 at 03:16
  • This [problem](https://stackoverflow.com/questions/604831/collection-was-modified-enumeration-operation-may-not-execute) looks similar to yours – jmesolomon Jun 14 '18 at 03:16
  • "*the build is success, it means the code is correct, right?*" - Wrong. Just because it compiles doesn't mean the code is going to work. "*every time I try the survive time is different*" - this is indicative that the problem is related to threading. The first step is to work out which collection is having the problem. Use the stack trace to find out which line it is coming from, and tell us. – Richardissimo Jun 14 '18 at 05:00
  • Hi All, I keep googling and find a solution for this problem. It says if you want to change the UI and the safest way is to use Invoke() or BeginInvoke(). After I use two invoke functions to take care of add and remove point, the red-cross had never showed again. – James Jun 25 '18 at 06:50

0 Answers0