开发者

Is Silverlight's CollectionViewSource thread-safe?

开发者 https://www.devze.com 2023-01-10 20:49 出处:网络
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 i开发者_JS百科s actually a

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 i开发者_JS百科s 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();
            });
    }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消