I have an issue with threading in my application.
As often with threading issue, it doesnt occur all the time.
Sometimes a collectionChanged exception occurs on this code :
SyncLock _padLock
System.Threading.Monitor.Enter(serie)
For Each dat In serie.Lignes
WriteLine(dat.Columns(), False, 0, False)'exception here
Next
System.Threading.Monitor.Exit(serie)
End SyncLock
Basically a serie contains lines, which are arrays of objects.
Why is another thread able to modify the series, if I have a monitor lock on that object ?
Also, I dont seem to be able to see when sections are locked in VS. Is there a specific way of doing so ?
Edit :
I modified the code so that the lock is done properly. That bug is gone. But now im trying to find out another bug im having.
This one is really tricky, because sometimes I get an unexpected result (highly due to a race) but I can never reproduce it while I have breakpoints in my code.
This is a really hairy situation.
Can you use .Net 4.0?
ConcurrentBag would be a better type to support your collection, so that is threadsafe.
You are currently locking twice (_padLock and serie), but this does not protect you against anyone modifying the collection without locking.
The other alternative could be to create a local copy of the collection and loop around this copy, but this does not eliminate the race.