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++;
}