Попробуйте это
public class ObservableCollectionThreadSafe<T> : ObservableCollection<T>
{
// Override the event so this class can access it
public override event NotifyCollectionChangedEventHandler CollectionChanged;
public ObservableCollectionThreadSafe()
{
}
public ObservableCollectionThreadSafe(IEnumerable<T> items)
: base(items)
{
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
// Be nice - use BlockReentrancy like MSDN said
using (BlockReentrancy())
{
NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;
if (eventHandler == null)
return;
Delegate[] delegates = eventHandler.GetInvocationList();
// Walk thru invocation list
foreach (NotifyCollectionChangedEventHandler handler in delegates)
{
DispatcherObject dispatcherObject = handler.Target as DispatcherObject;
// If the subscriber is a DispatcherObject and different thread
if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
{
// Invoke handler in the target dispatcher's thread
dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e);
}
else // Execute handler as is
handler(this, e);
}
}
}
}
РЕДАКТИРОВАТЬ: кстати, это не мой код, он был найден в сети кем-то ... так что "кто-то", если вы узнаете себя, вы должны взять кредит ...