I have a collection that has child collections and each of them can be updated from a different thread. How can I properly enable collection synchronization for all of them?
My current solution is to create an interface ISynchronized
that expose the object to lock when the collection is accessed:
public interface ISynchronized : IEnumerable
{
object SyncRoot { get; }
}
And then subscribe to BindingOperations.CollectionRegistering:
BindingOperations.CollectionRegistering += OnCollectionReg;
private void OnCollectionReg(object sender, CollectionRegisteringEventArgs e)
{
if (e.Collection is ISynchronized synchronized)
{
BindingOperations.EnableCollectionSynchronization(synchronized, synchronized.SyncRoot);
}
}
Are there pitfalls in this?