reactiveui / reactiveui/ReactiveUI

[Bug]: WPF thread error in v24

Open
#4,456 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
8.5k
Forks
1.2k
Avg merge
4d 2h
Merged PRs (30d)
6

Description

Describe the bug 🐞

CollectionView of this type does not support changes to its SourceCollection from threads other than the dispatcher thread.

Step to reproduce

Start WPF and trigger WhenAnyValue

public partial class MainWindow : Window, IViewFor<MainViewModel>
{
    public MainViewModel? ViewModel { get; set; }
    object? IViewFor.ViewModel
    {
        get => ViewModel;
        set => ViewModel = (MainViewModel)value!;
    }

    public MainWindow()
    {
        InitializeComponent();

        RxAppBuilder.CreateReactiveUIBuilder().WithWpf().BuildApp();
        DataContext = ViewModel = new();
        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.Bind(ViewModel, vm => vm.Text, v => v.TextBox.Text);
        this.Bind(ViewModel, vm => vm.Strings, v => v.ListBox.ItemsSource);
    }
}

public class MainViewModel : ReactiveObject
{
    public MainViewModel()
    {
        this.WhenAnyValue(x => x.Text)
            .Throttle(TimeSpan.FromSeconds(1), RxSchedulers.TaskpoolScheduler)
            .Where(x => string.IsNullOrWhiteSpace(x) is false)
            .ObserveOn(RxSchedulers.MainThreadScheduler)
            .Subscribe(x => Strings.Add(x));
    }

    public string Text
    {
        get;
        set => this.RaiseAndSetIfChanged(ref field, value, nameof(Text));
    }

    public ObservableCollection<string> Strings { get; set; } = new();
}

WpfApp1.zip

Reproduction repository

https://github.com/reactiveui/ReactiveUI

Expected behavior

Strings can be used normally with the Add method.
This code can works fine in ReactiveUI v23.2.28.

Screenshots 🖼️
Image
IDE

No response

Operating system

No response

Version

No response

Device

No response

ReactiveUI Version

24.2.0

Additional information ℹ️

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in the ReactiveUI repository by tracing the WPF binding path used by Bind for ObservableCollection and the MainThreadScheduler behavior after WhenAnyValue and Throttle. Compare v24.2.0 with v23.2.28 and use the supplied WPF reproduction to identify the regression; done means Strings.Add no longer raises the dispatcher-thread CollectionView error.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.