检查Dispatcher.CurrentDispatcher.Thread.ManagedThreadId后的CollectionView NotSupportedException

Tom*_*ter 1 c# wpf multithreading .net-4.0 task-parallel-library

我正在尝试用来Task.Factory.StartNew()运行后台操作.部分后台操作更新ObservableCollection中保存的对象.我正在使用从ObservableCollection派生的自定义类,以便OnCollectionChanged()在更改集合中某个对象上的属性时触发(请参阅/sf/answers/367977921/).如果CollectionView绑定了ObservableCollection,那么我得到一个异常:

System.NotSupportedException:这种类型的CollectionView不支持从与Dispatcher线程不同的线程更改其SourceCollection.

我试图避免这个异常,所以我添加了一些代码,只有OnCollectionChanged()在UI线程上运行才会触发.但不知怎的,我仍然得到例外..

这是我的ItemPropertyChanged()方法:

void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    var a = new NotifyCollectionChangedEventArgs(
        NotifyCollectionChangedAction.Reset);

    if (Thread.CurrentThread.ManagedThreadId
         == Dispatcher.CurrentDispatcher.Thread.ManagedThreadId)
    {
        OnCollectionChanged(a);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是完整的例外:

System.AggregateException was unhandled
  Message=A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.TaskExceptionHolder.Finalize()
       Message=This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
      InnerException: System.NotSupportedException
           Source=PresentationFramework
           StackTrace:
                at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
                at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
                at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
                at SourceLog.Model.TrulyObservableCollection`1.ItemPropertyChanged(Object sender, PropertyChangedEventArgs e) in C:\github.com\tomhunter-gh\SourceLog\SourceLog.Model\TrulyObservableCollection.cs:line 41
                at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
                at SourceLog.Model.LogEntry.OnPropertyChanged(String property) in C:\github.com\tomhunter-gh\SourceLog\SourceLog.Model\LogEntry.cs:line 44
                at SourceLog.Model.LogEntry.set_Read(Boolean value) in C:\github.com\tomhunter-gh\SourceLog\SourceLog.Model\LogEntry.cs:line 28
                at SourceLog.Model.LogEntry.<MarkAsReadAndSave>b__0() in C:\github.com\tomhunter-gh\SourceLog\SourceLog.Model\LogEntry.cs:line 53
                at System.Threading.Tasks.Task.InnerInvoke()
                at System.Threading.Tasks.Task.Execute()
           InnerException: 
Run Code Online (Sandbox Code Playgroud)

为什么当我明确检查到我是什么时,异常会抱怨我不在Dispatcher线程上?

H.B*_*.B. 6

Dispatcher.CurrentDispatcher与UI线程无关,因为属性的名称表明它为您提供当前线程的调度程序.所以你创建了一个总是返回true的检查.使用Application.Current.Dispatcher,而不是忽略您可以在所述调度程序上调用它的更改.