views:

48

answers:

1

I am trying to figure this out, given the following code, does the Refresh() need to occur on the UI thread? It seems to work, and I am wondering if the CollectionViewSource is actually a thread-aware / safe object? It definately has properties and methods to support calling on the correct thread, just not sure if that is left up to the developer, or if this is accomplished within the object?

public CollectionViewSource UserList { get; private set; }
    void setupCollections()
    {
        UserList = new CollectionViewSource();
        UserList.Source = searchProvider.UserResults;
        UserList.SortDescriptions.Add(new SortDescription("DisplayName", ListSortDirection.Ascending));
    }

Is this Thread safe in Silverlight???

void RefreshUserList()
    {
        UserList.View.Refresh();
    }

Or do you need to do something like this?

void RefreshUserList()
    {
        // Is This Required?
        UserList.Dispatcher.BeginInvoke(() =>
            {
                UserList.View.Refresh();
            });
        // Or MVVM-light Method
        DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                UserList.View.Refresh();
            });
    }
A: 

Per Microsoft's Documentation on CollectionViewSource the CollectionViewSource object is not Thread-safe. It seems that this is not reported to be Thread-safe, even though it does seems to work in many situations.

This may be because the method being called is actually on the View, not the CollectionViewSource. The View returns an ICollectionView interface - the details of the supporting class are not known, except that the CreateView() method creates this.

I would suggest that we always consider this not thread-safe and dispatch it to the correct thread, although my testing of the View.Refresh() at least suggests that it is thread-safe.

Ryan from Denver