I have an application where a DAQ system samples data at about 1 kHz which is then written to a DataTable.
The code block below shows part of the code which adds data to the datatable.
Public Sub AddTimeLoadData(DateTime As DateTime, DataPointNo As Integer, ClampForceN As Double, _
TransverseForceN As Double, TransverseDispMm As Double, NumLoadCycles As Integer)
Try
_tblTimeLoadData.AddtblTimeLoadDataRow(NumLoadCycles, TransverseDispMm, TransverseForceN, ClampForceN, DataPointNo, DateTime, _TimeLoadTestRow)
_timRaiseDataAdded.Start()
Catch ex As Exception
Throw New Exception("Time/load data could not be added: " & ex.Message, ex)
End Try
End Sub
Updating the GUI each time AddTimeLoadData is called would be unnecessary, so instead I start a System.Timers.Timer. At the Elapsed event of the timer, I raise the TimeLoadDataUpdated event.
In the GUI another class is listening to this event, and updates the chart. This works good most of the times, but occasionally I get an IndexOutOfRangeException when reading from the data table.
It is pretty clear that this has something to do with synchronization, but I have not yet figured out exactly what might be the issue. Looking at the code, the index 'i' would not really be able to go out of range. One idea that came up is that if the for loop reaches last row at the same time as it is being created I might have a problem so I could just try updating the for loop to
For i As Integer = s.Points.Count To sender.tblTimeLoadData.Count - 2
Or maybe there is something else that I have foreseen!?