Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async Task.Run with MVVM

I have been playing around with the new async CTP and MVVM patterns. I have been converting an old program of mine that was using a background worker and report progress to update a collection in my model. I have converted it to something like so

TaskEx.Run(async () =>
{
  while (true)
  {
    // update ObservableCollection here
  }
  await TaskEx.Delay(500);
});

In my view I bind to my viewmodel which exposes this observable collection. However, when my collection updates I get the following Exception

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.

I'm not sure what the correct way to pull the is back to the UI thread when done like this.

like image 990
poco Avatar asked Dec 10 '11 22:12

poco


2 Answers

You don't have to run async methods using Task.Run(), or any other special means, just call them. And in your case, that's exactly what is causing the problem.

Given function like this:

Action f = async () =>
{
    while (true)
    {
        // modify the observable collection here
        await Task.Delay(500);
    }
};

Calling it like this from some method run on the UI thread, like an event handler:

f();

works exactly as it should. It executes the first iteration of the cycle and then returns. The next iteration is executed after 500 ms (or more, if the UI thread is busy) on the UI thread.

On the other hand, if you call it like this:

Task.Run(addNames);

it doesn't work correctly. The reason for this is that async methods try to continue in the same context as they were started (unless you explicitly specify otherwise). The first version was started on the UI thread, so it continued on the UI thread. The second version started on a ThreadPool thread (thanks to Task.Run()) and continued there too. Which is why it caused your error.

All this is done using SynchronizationContext, if one is present.

like image 94
svick Avatar answered Sep 18 '22 01:09

svick


You created an ObservableCollection on the main UI thread, and are trying to update it on an asynchronous background thread, which you cannot do in WPF.

As an alternative, get the results from a background thread, then add them to the ObservableCollection on the main UI thread.

Usually my code for updating an ObservableCollection on a background thread will look something like this:

private async void LoadItems()
{
    Task<List<MyItem>> getItemsTask = Task.Factory.StartNew(GetItems);

    foreach(MyItem item in await getItemsTask)
        MyCollection.Add(item);
}

private List<MyItem> GetItems()
{
    // Make database call to get items
}
like image 25
Rachel Avatar answered Sep 19 '22 01:09

Rachel