WPF-支持异步操作的ObservableCollection-AsyncObservableCollection

在进行WPF开发过程中,需要从一个新的线程中操作ObservableCollection,结果程序抛出一个NotSupportedException的错误

public class AsyncObservableCollection<T> : ObservableCollection<T>
{
    //获取当前线程的SynchronizationContext对象
    private SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
    public AsyncObservableCollection() { }
    public AsyncObservableCollection(IEnumerable<T> list) : base(list) { }
    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
          
        if (SynchronizationContext.Current == _synchronizationContext)
        {
            //如果操作发生在同一个线程中,不需要进行跨线程执行         
            RaiseCollectionChanged(e);
        }
        else
        {
            //如果不是发生在同一个线程中
            //准确说来,这里是在一个非UI线程中,需要进行UI的更新所进行的操作         
            _synchronizationContext.Post(RaiseCollectionChanged, e);
        }
    }
    private void RaiseCollectionChanged(object param)
    {
        // 执行         
        base.OnCollectionChanged((NotifyCollectionChangedEventArgs)param);
    }
    protected override void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (SynchronizationContext.Current == _synchronizationContext)
        {
            // Execute the PropertyChanged event on the current thread             
            RaisePropertyChanged(e);
        }
        else
        {
            // Post the PropertyChanged event on the creator thread             
            _synchronizationContext.Post(RaisePropertyChanged, e);
        }
    }
    private void RaisePropertyChanged(object param)
    {
        // We are in the creator thread, call the base implementation directly         
        base.OnPropertyChanged((PropertyChangedEventArgs)param);
    }
}
View Code

https://blog.csdn.net/juns6/article/details/50964496

猜你喜欢

转载自www.cnblogs.com/tianciliangen/p/9179478.html
今日推荐