Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asynchronous UI update from ViewModel in WPF

I am having a problem with getting data from db and showing in UI asynchronously. I am using MVVM light, when I click the button, action is triggered in ViewModel:

    private void SearchQuery(string query)
    {
        _redisModel.GetFriendsListAsync(query);
    } 

At some point GetFriendsListCompleted is called by background thread notifing viewmodel that job is done. At this point I need to update ListBox ItemSource. But when I try to update is I get “The calling thread cannot access this object because a different thread owns it” I have tried Dispatcher.CurrentDispatcher.Invoke(),App.Current.Dispatcher.Invoke() and different magic, but it still doesn’t work.

I tried to give UI dispatcher to ViewModel and then call it from there - didn't work.

private string filterText = string.Empty;
    public string FilterText
    {
        get { return filterText; }
        set
        {
            filterText = value;
            this.RaisePropertyChanged(() => this.FilterText);

            this.FriendsList.View.Refresh(); // Here where exception is happening.
        }
    }

I tried to change this line to

Dispatcher.Invoke(DispatcherPriority.Normal, new Action( () =>this.FriendsList.View.Refresh())); - still the same.

I am using Telerik ListBox to display items. FriendList is CollectionViewSource(http://www.telerik.com/help/wpf/radlistbox-overview.html). It works when I use Telerik example from WPF Control Examples. Problems start to occur when I use my async methods. Type of view is System.ComponentModel.ICollectionView it is used for Filtering and Grouping.

I have also tried to just assign ObservableCollection to Items property of the ListBox and it doesn't work either.

A bit more details on how _redisModel.GetFriendsListAsync works: In the end(after all chain of calls) it ends up here:

public GetAsyncResult(Func<T> workToBeDone, Action<IAsyncResult> cbMethod, Object state)
{
   _cbMethod = cbMethod;
   _state = state;
   QueueWorkOnThreadPool(workToBeDone);
}

ThreadPool.QueueUserWorkItem(state =>
{
  try
  {
     _result = workToBeDone();
  }
  catch (Exception ex)
  {
       _exception = ex;
  }
  finally
  {
     UpdateStatusToComplete(); //1 and 2 
     NotifyCallbackWhenAvailable(); //3 callback invocation 
  }
 });

In viewmodel I have method:

private void GetFriendsListCompleted(object sender, ResultsArgs<Friend> e)
    {
        if (!e.HasError)
        {
            var curr = e.Results;
            if (curr != null)
            {
                this.FriendsList= new CollectionViewSource();

                this.FriendsList.Source = list;
                this.FriendsList.Filter += this.FriendFilter;
                FilterText = "";

                Dispatcher.Invoke(DispatcherPriority.Normal, new Action(

                        () => this.FriendsList.View.Refresh()));
            }
    }

Can anybody please help me with this ? Thank you

like image 510
Andrey Ant Avatar asked Oct 04 '22 20:10

Andrey Ant


1 Answers

You are creating CollectionViewSource in one thread and refreshing that in another thread (dispatcher thread). Update your GetFriendsListCompleted to

private void GetFriendsListCompleted(object sender, ResultsArgs<Friend> e)
{
    if (!e.HasError)
    {
        var curr = e.Results;
        if (curr != null)
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
                    () => {
                     this.FriendsList= new CollectionViewSource();
                     this.FriendsList.Source = list;
                     this.FriendsList.Filter += this.FriendFilter;
                     FilterText = "";
                     this.FriendsList.View.Refresh();
                     }));
        }
    }
}
like image 147
Suresh Avatar answered Oct 12 '22 10:10

Suresh