Silverlight的CollectionViewSource是线程安全的吗?

发布于 2024-09-13 12:10:49 字数 996 浏览 7 评论 0原文

我试图弄清楚这一点,给定以下代码,Refresh() 是否需要在 UI 线程上发生?它似乎有效,我想知道 CollectionViewSource 是否实际上是一个线程感知/安全对象?它肯定具有支持在正确线程上调用的属性和方法,只是不确定这是否由开发人员决定,或者是否在对象内完成?

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

这个线程在 Silverlight 中安全吗???

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

或者你需要做这样的事情吗?

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

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();
            });
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2024-09-20 12:10:49

根据 Microsoft 的文档 CollectionViewSource CollectionViewSource 对象不是线程安全的。据报道,这似乎不是线程安全的,尽管它似乎在许多情况下都有效。

这可能是因为被调用的方法实际上是在 View 上,而不是 CollectionViewSource 上。该视图返回 ICollectionView 接口 - 支持类的详细信息未知,除了 CreateView() 方法创建它之外。

我建议我们始终认为这不是线程安全的,并将其分派到正确的线程,尽管我对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文