You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ListBox collections have varied IsSynchronized method implementations #3088
@hughbe I'd make this match the underlying behavior. If we want to make any of these truly synchronized, we can look at them individually as new features.
Note that writing code which does this generically (i.e. can deal both with an underlying collection that IsSynchronized has true or false) is hard.
If the underlying collection has IsSynchronized==true then you can only forward methods which consist of a single access to the underlying collection. Methods which have more than one access to the underlying collection need an additional lock (_underlyingCollection.SyncObject) to keep the claim of a forwarded IsSynchronized==true correct - otherwise another thread may interleave between the two accesses (which themselves are threadsave) making the calling code not threadsafe because the operation no longer is atomic in regards to the SyncObject.
Note that this lock is only to be done if the underlying collection claims IsSynchronized==true, if its false then this lock should not be done.
Writing a wrapper which is aware of its underlying collection (e.g. because he created it himself) is much easier since you can special case your implementation for either case and don't have to conditionally lock.
If you have to deal with arbitrary underlying collections you can always claim IsSynchronized==false instead of writing complex conditional locks.
Disclaimer: its been 5+ years since I last dealt with synchronized collections (back when ASP.NET WebForms was still a thing ...) - I may remember things incorrectly.
I mainly wanted to put out a warning that plain forwarding of IsSynchronized can easily be incorrect if the subclass/wrapper has additional logic.
Activity
RussKie commentedon Apr 20, 2020
I would really appreciate if you use the templates when raising new issues. It makes triaging issues significantly easier.
hughbe commentedon Apr 20, 2020
@RussKie my apologies, will use it going forward
JeremyKuhne commentedon May 14, 2020
@hughbe I'd make this match the underlying behavior. If we want to make any of these truly synchronized, we can look at them individually as new features.
weltkante commentedon May 15, 2020
Note that writing code which does this generically (i.e. can deal both with an underlying collection that IsSynchronized has true or false) is hard.
If the underlying collection has
IsSynchronized==truethen you can only forward methods which consist of a single access to the underlying collection. Methods which have more than one access to the underlying collection need an additionallock (_underlyingCollection.SyncObject)to keep the claim of a forwardedIsSynchronized==truecorrect - otherwise another thread may interleave between the two accesses (which themselves are threadsave) making the calling code not threadsafe because the operation no longer is atomic in regards to the SyncObject.Note that this lock is only to be done if the underlying collection claims
IsSynchronized==true, if its false then this lock should not be done.Writing a wrapper which is aware of its underlying collection (e.g. because he created it himself) is much easier since you can special case your implementation for either case and don't have to conditionally lock.
If you have to deal with arbitrary underlying collections you can always claim
IsSynchronized==falseinstead of writing complex conditional locks.Disclaimer: its been 5+ years since I last dealt with synchronized collections (back when ASP.NET WebForms was still a thing ...) - I may remember things incorrectly.
I mainly wanted to put out a warning that plain forwarding of IsSynchronized can easily be incorrect if the subclass/wrapper has additional logic.