繁体   English   中英

System.Reactive.dll 中的 System.NotSupportedException

[英]System.NotSupportedException in System.Reactive.dll

我是 Rx.Net 和动态数据的新手,我目前使用这些反应式 UI 扩展面临以下问题。

我试图达到的目标:

  1. TaskpoolScheduler上应用 Dynamic 的 Data Filter操作(或者一般异步过滤传入的数据)
  2. 从后台线程向SourceList添加数据

但是,当SourceList填充来自不同任务的数据时,我'System.NotSupportedException' in System.Reactive.dll 所以我必须使用调度程序线程。

如何解决这个问题?

一个最小的工作示例:

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 ,如下<ListBox ItemsSource="{Binding Items}"/>

你有没有尝试过

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

也许值得尝试一下。

另一种可能的解决方案如下:

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

        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM