首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.NotSupportedException in System.Reactive.dll

System.NotSupportedException in System.Reactive.dll
EN

Stack Overflow用户
提问于 2020-06-23 19:42:29
回答 2查看 122关注 0票数 0

我对Rx.Net和动态数据很陌生,现在通过使用这些反应性UI扩展,我面临着以下问题。

我所努力达到的目标:

  1. TaskpoolScheduler上应用动态的数据Filter操作(或者通常从后台线程

将传入的数据Filter数据过滤到SourceList

但是,当SourceList被来自不同任务的数据填充时,我会得到'System.NotSupportedException' in System.Reactive.dll错误。所以我得用调度员线程。

怎么解决这个问题?

一个最低限度的工作示例:

代码语言:javascript
复制
using DynamicData;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ReactiveExtensionsTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly ReadOnlyObservableCollection<DataModel> _items;

        public ReadOnlyObservableCollection<DataModel> Items => _items;

        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;

            var bgDataService = new BackroundDataService();
            bgDataService
                .Connect()
                .ObserveOn(RxApp.TaskpoolScheduler)
                .Filter(x => x.IntA % 2 == 0)
                .ObserveOn(RxApp.MainThreadScheduler)
                .Bind(out _items)
                .Subscribe();
        }


    }

    class BackroundDataService : IBackgroundDataService
    {
        private readonly SourceList<DataModel> _data = new SourceList<DataModel>();

        public IObservable<IChangeSet<DataModel>> Connect() => _data.Connect();

        public BackroundDataService()
        {
            Task.Factory.StartNew(async () =>
            {
                int length = 100_000;
                for (int i = 0; i < length; i++)
                {
                    _data.Add(new DataModel { IntA = i, StringB = "B {i}" });
                    await Task.Delay(200);
                }
            });
        }

    }

    internal interface IBackgroundDataService
    {
        IObservable<IChangeSet<DataModel>> Connect();
    }

    public class DataModel
    {
        public int IntA { get; set; }

        public string StringB { get; set; }
    }
}

Items属性绑定到ListBox,如下所示

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-24 13:03:03

你试过吗

代码语言:javascript
复制
    var bgDataService = new BackroundDataService();
            bgDataService
                .Connect()
                .ObserveOn(RxApp.TaskpoolScheduler)
                .Filter(x => x.IntA % 2 == 0)
                .Bind(out _items)
                .SubscribeOn(RxApp.MainThreadScheduler);

也许值得一试。

票数 0
EN

Stack Overflow用户

发布于 2020-06-25 18:08:00

另一种可能的解决办法如下:

代码语言:javascript
复制
    var bgDataService = new BackroundDataService();
    bgDataService
        .Connect()
        .ObserveOn(RxApp.TaskpoolScheduler)
        .Filter(x =>
        {
            Thread currentThread = Thread.CurrentThread;
            Console.WriteLine($"Applied filter on thread: {currentThread.GetApartmentState()} with Id: "
                              + $"{currentThread.ManagedThreadId} and is background: {currentThread.IsBackground} and from thread pool: {currentThread.IsThreadPoolThread}");
            return x.IntA % 2 == 0;
        })
        .ObserveOnDispatcher()
        .Bind(out _items)
        .Subscribe(x =>
        {
            Thread currentThread = Thread.CurrentThread;
            Console.WriteLine($"Subscribption runs on thread: {currentThread.GetApartmentState()} with Id: "
                              + $"{currentThread.ManagedThreadId} and is background: {currentThread.IsBackground} and from thread pool: {currentThread.IsThreadPoolThread}");

        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62542554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档