How to make ObservableCollection thread-safe?

As of .net framwork 4.5 you can use native collection synchronization. BindingOperations.EnableCollectionSynchronization(YourCollection, YourLockObject); YourLockObject is instance of any object e.g. new Object();. Use one per collection. This eliminates the need of some special class or anything. Just enable and enjoy 😉 [edit] As stated in the comments by Mark and Ed (thanks for clarifying!), this … Read more

ObservableCollection and Item PropertyChanged

Here is how you would attach/detach to each item’s PropertyChanged event. ObservableCollection<INotifyPropertyChanged> items = new ObservableCollection<INotifyPropertyChanged>(); items.CollectionChanged += items_CollectionChanged; static void items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.OldItems != null) { foreach (INotifyPropertyChanged item in e.OldItems) item.PropertyChanged -= item_PropertyChanged; } if (e.NewItems != null) { foreach (INotifyPropertyChanged item in e.NewItems) item.PropertyChanged += item_PropertyChanged; } } … Read more

ObservableCollection and threading [duplicate]

The best way to solve this is to pass the Dispatcher object to the start method of the background thread. void DoBackgroundOperation(ObservableCollection<SomeType> col) { var dispatcher = Dispatcher.CurrentDispatcher; ThreadStart start = () => BackgroundStart(dispatcher, col); var t = new Thread(start); t.Start(); } private static void BackgroundStart( Dispatcher dispatcher, ObservableCollection<SomeType> col) { … SomeType t = … Read more

.NET ObservableDictionary

The Microsoft ParallelExtensionsExtras provides this class which is not only observable but is also concurrent: Now available via Nuget: https://www.nuget.org/packages/MSFT.ParallelExtensionsExtras/ The source code was recently updated for .NET Standard 2.1 on 05/11/2020 and the source code is available at GitHub: https://github.com/dotnet/samples/tree/master/csharp/parallel/ParallelExtensionsExtras Note that some of the features are now a part of newer .NET frameworks. … Read more

How to Avoid Firing ObservableCollection.CollectionChanged Multiple Times When Replacing All Elements Or Adding a Collection of Elements

ColinE is right with all his informations. I only want to add my subclass of ObservableCollection that I use for this specific case. public class SmartCollection<T> : ObservableCollection<T> { public SmartCollection() : base() { } public SmartCollection(IEnumerable<T> collection) : base(collection) { } public SmartCollection(List<T> list) : base(list) { } public void AddRange(IEnumerable<T> range) { foreach … Read more

Fast performing and thread safe observable collection

ObservableCollection can be fast, if it wants to. 🙂 The code below is a very good example of a thread safe, faster observable collection and you can extend it further to your wish. using System.Collections.Specialized; public class FastObservableCollection<T> : ObservableCollection<T> { private readonly object locker = new object(); /// <summary> /// This private variable holds … Read more

Sort ObservableCollection through C#

Introduction Basically, if there is a need to display a sorted collection, please consider using the CollectionViewSource class: assign (“bind”) its Source property to the source collection — an instance of the ObservableCollection<T> class. The idea is that CollectionViewSource class provides an instance of the CollectionView class. This is kind of “projection” of the original … Read more

Notify ObservableCollection when Item changes

The spot you have commented as // Code to trig on item change… will only trigger when the collection object gets changed, such as when it gets set to a new object, or set to null. With your current implementation of TrulyObservableCollection, to handle the property changed events of your collection, register something to the … Read more

tech