测量工作室图表,集合被修改异常
Measurement studio graphs, Collection was modified exception
我对 measurement studio dll 的图形(我几乎没有这个 class 的实例)有疑问。发生几次后,我得到 Collection was modified; enumeration operation may not execute.
但异常未处理,所以我无法确切地看到它发生的位置。我在互联网上发红是因为我没有在 UI 线程中提供图表,但我认为我不在 UI 线程之外:
private delegate void del(double press, double temp, double press1);
private object graphic_mutex = new object();
private void UpdateView(double press, double temp, double press1)
{
if (InvokeRequired)
Invoke(new del (UpdateView), new object[] { press , temp, press1});
lock (graphic_mutex)
{
_PressionLine.PlotYAppend(press);
_TemperatureLine.PlotYAppend(temp);
if (_isdoublePressureSensor == true)
_PressionLine2.PlotYAppend(press1);
}
}
我得到的确切错误:
Exception thrown: 'System.InvalidOperationException' in mscorlib.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Collection was modified; enumeration operation may not execute.
你有解决这个问题的办法吗?
您应该 return 在调用之后。当 InvokeRequired 为真时,您不希望代码执行两次。代码可以直接执行,也可以通过调用执行。两者都不是。
private delegate void del(double press, double temp, double press1);
private object graphic_mutex = new object();
private void UpdateView(double press, double temp, double press1)
{
if (InvokeRequired)
{
Invoke(new del (UpdateView), new object[] { press , temp, press1});
return;
}
lock (graphic_mutex)
{
_PressionLine.PlotYAppend(press);
_TemperatureLine.PlotYAppend(temp);
if (_isdoublePressureSensor == true)
_PressionLine2.PlotYAppend(press1);
}
}
不需要锁,因为它将(必须)始终在 UI 线程上执行。
我对 measurement studio dll 的图形(我几乎没有这个 class 的实例)有疑问。发生几次后,我得到 Collection was modified; enumeration operation may not execute.
但异常未处理,所以我无法确切地看到它发生的位置。我在互联网上发红是因为我没有在 UI 线程中提供图表,但我认为我不在 UI 线程之外:
private delegate void del(double press, double temp, double press1);
private object graphic_mutex = new object();
private void UpdateView(double press, double temp, double press1)
{
if (InvokeRequired)
Invoke(new del (UpdateView), new object[] { press , temp, press1});
lock (graphic_mutex)
{
_PressionLine.PlotYAppend(press);
_TemperatureLine.PlotYAppend(temp);
if (_isdoublePressureSensor == true)
_PressionLine2.PlotYAppend(press1);
}
}
我得到的确切错误:
Exception thrown: 'System.InvalidOperationException' in mscorlib.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Collection was modified; enumeration operation may not execute.
你有解决这个问题的办法吗?
您应该 return 在调用之后。当 InvokeRequired 为真时,您不希望代码执行两次。代码可以直接执行,也可以通过调用执行。两者都不是。
private delegate void del(double press, double temp, double press1);
private object graphic_mutex = new object();
private void UpdateView(double press, double temp, double press1)
{
if (InvokeRequired)
{
Invoke(new del (UpdateView), new object[] { press , temp, press1});
return;
}
lock (graphic_mutex)
{
_PressionLine.PlotYAppend(press);
_TemperatureLine.PlotYAppend(temp);
if (_isdoublePressureSensor == true)
_PressionLine2.PlotYAppend(press1);
}
}
不需要锁,因为它将(必须)始终在 UI 线程上执行。