Description
Describe the bug 🐞
If an object has multiple properties that observe another object with WhenAnyValue().ToProperty()
, and some (but not all) of them use ObserveOn
to ensure they are updated on the UI thread, they are sometimes updated on background threads anyway.
I think the minimal scenario is:
- A ReactiveObject ("business object") has one or more properties which get updated by a worker thread.
- Another ReactiveObject ("view model") has a property P1 which is linked to the business object by
WhenAnyValue(...).ObserveOn(RxApp.MainThreadScheduler).ToProperty()
. - A third object ("view") observes P1, and its observer needs to run on the main thread.
- But the view model also has a property P2 which is linked to the same business object, and does not use
ObserveOn
(or uses it with some other scheduler).
If the business object's property change events fire too rapidly, the binding will try to update the view on a background thread, which causes a crash under WPF.
Step to reproduce
Run this test.
Case 2, where Counter2
observes the business object on CurrentThreadScheduler
, will fail somewhere in the first few hundred iterations.
(The same behavior happens if the scheduler is passed to ToProperty
.)
Reproduction repository
https://github.com/reactiveui/ReactiveUI
Expected behavior
I expect that a property defined with ObserveOn(MainThreadScheduler).ToProperty()
will always update on the main thread.
Screenshots 🖼️
No response
IDE
No response
Operating system
Windows 11
Version
No response
Device
No response
ReactiveUI Version
18.3.1 and 19.4.1
Additional information ℹ️
The real case where this came up is in a WPF app that uses a mix of ReactiveUI bindings (to view model properties that marshal to the UI thread) and XAML bindings (to properties that don't marshal, because that's allowed). I'm aware this is not the recommended way to build view models.