observablecollection thread-safe code example
Example: making observablecollection thread safe
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows;
using System.Threading;
public static object m_lockObject;
public static ObservableCollection<T> m_OC;
OC = new ObservableCollection<T>();
BindingOperations.EnableCollectionSynchronization(m_OC, m_lockObject);
Application.Current.Dispatcher.Send(
DispatcherPriority.Background,
new Action(() => {
BindingOperations.EnableCollectionSynchronization(m_OC, m_lockObject);
}));
lock (m_lockObject)
{
m_OC.Add(null);
}
Application.Current?.Dispatcher?.Invoke(() =>
{
});