VB异常集合已修改;枚举操作可能无法执行



我正在尝试学习VB编程,并一直在使用Arduino和Visual Basic Forms PC GUI进行云检测项目。我已经取得了很大的进步,但我偶尔会遇到一个错误:

************** Exception Text **************
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOper ationException(ExceptionResource resource)
at System.Collections.Generic.List`1.E numerator.MoveNextRare()
at System.Collections.Generic.List`1.E numerator.MoveNext()
at System.Windows.Forms.DataVisualizat ion.Charting.ChartTypes.LineChart.P rocessChartType(Boolean selection, ChartGraphics graph, CommonElements common, ChartArea area, Series seriesToDraw)
at System.Windows.Forms.DataVisualizat ion.Charting.ChartTypes.LineChart.P aint(ChartGraphics graph, CommonElements common, ChartArea area, Series seriesToDraw)
at System.Windows.Forms.DataVisualizat ion.Charting.ChartArea.Paint(ChartG raphics graph)
at System.Windows.Forms.DataVisualizat ion.Charting.ChartPicture.Paint(Gra phics graph, Boolean paintTopLevelElementOnly)
at System.Windows.Forms.DataVisualizat ion.Charting.Chart.OnPaint(PaintEve ntArgs e)
at System.Windows.Forms.Control.PaintW ithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPain t(Message& m)
at System.Windows.Forms.Control.WndPro c(Message& m)
at System.Windows.Forms.Control.Contro lNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.Contro lNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.C allback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我在谷歌上花了很多时间试图理解这个问题并找到解决方案,但我的经验不足,无法实施解决这个问题所需的措施。我认为问题在于更新用于在我的PC GUI上显示三个图表的数据。我猜数据更新的线程与绘制图表的线程不同,但我不清楚如何处理这个问题。

我的代码分为一系列子程序:

ProcessData() ' do the basic calcs
CloudCalcs() ' do the cloud calculations
UpdateHistory() ' update the graphs

图形更新的子程序为:

Private Sub UpdateHistory()
time = TimeOfDay() ' retrieves current system time (used for plot x axis)
Chart1.Series("Series1").Points.Add XY(time, cloud)
Chart2.Series("Series1").Points.Add XY(time, light)
Chart3.Series("Series1").Points.Add XY(time, wind)
If HistoryFull = False Then
j = j + 1
End If
' this starts removing the oldest data points when the required number of points have been added to the history
If j > hpt Then
HistoryFull = True
Chart1.Series("Series1").Points.RemoveAt(0)
Chart2.Series("Series1").Points.RemoveAt(0)
Chart3.Series("Series1").Points.RemoveAt(0)
End If
End Sub

如果有人能指导我如何解决这个问题,我将不胜感激。

谢谢,

Peter

得到了一些帮助-成功了:

Private Sub UpdateHistory()
time = TimeOfDay() ' retrieves current system time (used for plot x axis)
AddDataPoint(Chart1, time, cloud)
AddDataPoint(Chart2, time, light)
AddDataPoint(Chart3, time, wind)
If HistoryFull = False Then
j = j + 1
End If
If j > hpt Then
HistoryFull = True
RemoveDataPoint(Chart1)
RemoveDataPoint(Chart2)
RemoveDataPoint(Chart3)
End If
End Sub
Friend Delegate Sub RemoveDataPointCallback(ByRef chart As Chart)
Private Sub RemoveDataPoint(ByRef chart As Chart)
If chart.InvokeRequired Then
Dim callback As RemoveDataPointCallback = New RemoveDataPointCallback(AddressOf RemoveDataPoint)
Me.Invoke(callback, New Object() {chart})
Else
chart.Series("Series1").Points.RemoveAt(0)
End If
End Sub

Friend Delegate Sub AddDataPointCallback(ByRef chart As Chart, chartTime As Date, chartData As Double)
Private Sub AddDataPoint(ByRef chart As Chart, chartTime As Date, chartData As Double)
If chart.InvokeRequired Then
Dim callback As AddDataPointCallback = New AddDataPointCallback(AddressOf AddDataPoint)
Me.Invoke(callback, New Object() {chart, chartTime, chartData})
Else
chart.Series("Series1").Points.AddXY(chartTime, chartData)
End If
End Sub

最新更新