InvalidOperationException while iterating over AssemblyReferences collection

SyntaxEditor .NET Languages Add-on for WPF Forum

Posted 2 years ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Version: 22.1.1
Platform: .NET 4.8
Environment: Windows 10 (64-bit)
Avatar

Hi,

we experienced an InvalidOperationException while iterating over AssemblyReferences collection, because the collection was changed in a different thread. I was a bit irritated, because I saw that the collection inherits from SimpleSynchronizedCollection. Given the name, I assumed the collection was thread-safe. When I look at the code, I can see there are locks when changing the collection, but not while iterating over it. So you can safely change the collection from different threads, but not read from one thread and change it on a different thread.

Was this a design choice to improve the performance? In my opinion this defeats the whole purpose. If parallel read-access should be allowed, I would suggest you use ReaderWriterLockSlim instead of the default lock/monitor.


Best regards, Tobias Lingemann.

Comments (1)

Answer - Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tobias,

I can see how this would be frustrating, and we certainly appreciate the feedback/suggestion. The intention of this class (which was written a long time ago) was to provide a lightweight derivative to Collection<T> that could report changes (i.e. observable) and be generally thread-safe. It has served this purpose well. Enumerating over a collection is ultimately a series of mulitple read calls and even locking each item as it is read would not block a write from happening between reads unless a read lock was kept in place for the entire enumeration.

The best way to ensure the collection is not modified while you iterate it is to use a standard lock/monitor on the SyncRoot of the collection. This will ensure no updates are made to the collection until you are done iterating it or have copied the items to another collection for iteration.

Any updates to support more concurrency scenarios around enumeration would require breaking changes.


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.