peter_4059
07-03-2020, 10:44 AM
I've decided to post this again here as I don't think it is getting noticed in the parent thread. I've been working on an arduino cloud detector project, mainly to learn how arduinos work and also to learn some basic computer programming. I've created a desktop application using visual studio VB forms to receive the sensor data from the arduino, do the cloud prediction calcs and display the information. It is all working well except I get an occasional error in the VB app. The error appears to be related to updating one of the three plots I have on the VB form.
I get the following exception code when it occurs:
************** 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)
I've done the obvious Google search and have arrived at the conclusion this error is related to updating or modifying the chart from a different thread than it was created on.
My code is divided into a series of subroutines:
ProcessData() ' do the basic calcs
CloudCalcs() ' do the cloud calculations
UpdateHistory() ' update the graphs
The subroutine for the graph updates is:
Private Sub UpdateHistory()
time = TimeOfDay() ' retrieves current system time (used for plot x axis)
Chart1.Series("Series1").Points.AddXY(time, cloud)
Chart2.Series("Series1").Points.AddXY(time, light)
Chart3.Series("Series1").Points.AddXY(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
I've tried various things to stop it executing other parts of the code while the UpdateHistory() part is running but haven't managed to avoid the error yet.
Just wondering if anyone with more programming experience has any tips?
Thanks
Peter
I get the following exception code when it occurs:
************** 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)
I've done the obvious Google search and have arrived at the conclusion this error is related to updating or modifying the chart from a different thread than it was created on.
My code is divided into a series of subroutines:
ProcessData() ' do the basic calcs
CloudCalcs() ' do the cloud calculations
UpdateHistory() ' update the graphs
The subroutine for the graph updates is:
Private Sub UpdateHistory()
time = TimeOfDay() ' retrieves current system time (used for plot x axis)
Chart1.Series("Series1").Points.AddXY(time, cloud)
Chart2.Series("Series1").Points.AddXY(time, light)
Chart3.Series("Series1").Points.AddXY(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
I've tried various things to stop it executing other parts of the code while the UpdateHistory() part is running but haven't managed to avoid the error yet.
Just wondering if anyone with more programming experience has any tips?
Thanks
Peter