Reputation: 2417
I want to load my CollectionViewSource
asynchronously. So I wrote such code:
list1 = new List<int>();
list2 = new List<int>();
Task.Factory.StartNew<Tuple<List<int>, List<int>>>(() =>
{
// Create and return tuple with 2 lists
}).ContinueWith(doneTask =>
{
list1 = doneTask.Result.Item1;
list2 = doneTask.Result.Item2;
// update UI
collectionViewSource1.Source = list1;
collectionViewSource2.Source = list2;
}, TaskScheduler.FromCurrentSynchronizationContext());
But this code doesn't work.
Exception System.ArgumentException: Must create DependencySource on same Thread as the DependencyObject.
occurs.
Upvotes: 2
Views: 829
Reputation: 184777
DependencyObjects have thread affinity, you cannot modify them on a background thread. You should be able to do this using the application's Dispatcher like this:
App.Current.Dispatcher.Invoke((Action)delegate
{
collectionViewSource1.Source = list1;
collectionViewSource2.Source = list2;
}, null);
This article on MSDN might provide more relevant information.
Upvotes: 1