如何避免“收集被修改;枚举操作可能不会执行。在刷新C#图表期间? [英] How to avoid "collection was modified; enumeration operation may not execute." during refresh C# chart?

查看:59
本文介绍了如何避免“收集被修改;枚举操作可能不会执行。在刷新C#图表期间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



当我尝试使用一段时间并且不断刷新图表时我遇到了这个问题。

起初,我使用函数:AddY()和函数:removeAt()来更新最新的数据,例如示例1.



我也尝试使用数组来保存数据并自行左移数据,并添加最新数据。刷新数组后我使用函数:DataBindY()来绘制图表。请参考示例2.



构建期间没有错误,一开始功能正常,等待一个随机时间,图表会变成红色交叉并跳出错误消息,说收集已被修改;枚举操作可能无法执行。



构建成功有两件奇怪的事情,这意味着代码是正确的,对吗?其次是随机时间,每次我尝试的生存时间都不同。这真的没有意义。我在网上搜索了这个错误信息,但我找不到熟悉的情况。



有没有人遇到同样的情况可以帮我这个? br $> b $ b

Thx。



BTW,我的窗口系统是Win7,C#的版本是2010.


我尝试过:



示例1

Hi all,

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.

What I have tried:

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





示例2



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

推荐答案

集合被修改异常意味着一个线程正在循环一个集合,该集合位于同时在另一个线程中修改。



看起来你正在修改后台线程中的图表数据,这是不允许的。





The "Collection was modified" exception means that one thread is looping over a collection, which is at the same time modified in another thread.

It looks like you are modifying the chart's data from a background thread, which is not allowed.


引用:

构建成功,这意味着代码是正确的,对吗?

the build is success, it means the code is correct, right?

这绝对不是真的。如果构建成功,则仅表示没有语法错误。它没有说明它是否会做你想要的;编译器无法知道。

This is most definitely not true. If the build succeeds, it merely means there are no syntax errors. It says nothing about whether or not it will do what you want; the compiler can't know that.


这篇关于如何避免“收集被修改;枚举操作可能不会执行。在刷新C#图表期间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆