I'm trying to use Task.Factory.StartNew()
to run a background operation. Part of the background operation updates an object that's held in an ObservableCollection. I'm using a custom class derived from ObservableCollection to fire OnCollectionChanged()
when a property on one of the objects in the collection is changed (see https://stackoverflow.com/a/5256827/62072). If there is a CollectionView
bound to the ObservableCollection then I get an exception:
System.NotSupportedException: This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
I'm trying to avoid this exception so I've added some code to only fire OnCollectionChanged()
if running on the UI thread. But somehow I still get the exception..
Here's my ItemPropertyChanged()
method:
void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var a = new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Reset);
if (Thread.CurrentThread.ManagedThreadId
== Dispatcher.CurrentDispatcher.Thread.ManagedThreadId)
{
OnCollectionChanged(a);
}
}
And here's the full exception:
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:
How come the exception complains that I'm not on the Dispatcher thread when I've explicitly checked that I am?