Reputation: 1503
VB.NET, .NET 4
Hello all,
I have a List(Of Byte) that is filled with bytes from the serial buffer on a SerialPort.DataRecieved Event. I then try to parse the data. Part of the parsing process involves deleting elements of the List(Of Byte). Should I be concerned about the List being modified by a DataRecieved Event that might be raised during the parsing process? I realize that probably depends on what I'm trying to do, but, assuming I should be concerned (e.g., the parsing process needs List.Count to not change until parsing is finished), how should I go about making sure any Add calls wait until the parser is done? I guess the answer is something like SyncLock, but I've never really understood how SyncLock works. Any basic help would be appreciated!
Thanks in advance, Brian
Upvotes: 0
Views: 746
Reputation: 941615
Well, it's not the greatest use of CPU cycles, removing bytes from a List(Of Byte) is an O(n) operation. Making the overall processing step O(n^2). It is still quite difficult to put any kind of pressure on the cpu doing so, serial ports are glacially slow. You should only ever modify working code if you have measured it to be a perf problem.
If you're not there yet then consider creating a new array or List from the old one. That's O(n), the extra storage cannot hurt considering the slow data rates. The code should be cleaner too.
As far as threading goes, be sure to do this in the DataReceived handler. That's thread-safe and avoids putting undue pressure on the UI thread in case you invoke.
Upvotes: 2