Commit fed5931
## Summary
Fixes #4354 — recurrence of #3886 and #4180.
`RxSchedulers.MainThreadScheduler` (registered as
`WaitForDispatcherScheduler` after `WithWpf()` bootstrap) was silently
falling back to `CurrentThreadScheduler.Instance` when `Schedule()` was
called from a background thread. This made
`ObserveOn(RxSchedulers.MainThreadScheduler)` a no-op from background
threads, causing cross-thread `InvalidOperationException` crashes on WPF
controls (e.g. `MenuItem.UpdateCanExecute`).
## Root Cause
`WpfMainThreadScheduler` used `DispatcherScheduler.Current` as its
factory:
```csharp
// Before
public static IScheduler WpfMainThreadScheduler { get; } =
new WaitForDispatcherScheduler(static () => DispatcherScheduler.Current);
```
`DispatcherScheduler.Current` internally calls
`Dispatcher.FromThread(Thread.CurrentThread)`. This **always returns
null on background threads** (pool threads have no WPF Dispatcher),
causing an `InvalidOperationException`.
`WaitForDispatcherScheduler.AttemptToCreateScheduler()` catches this,
returns `CurrentThreadScheduler.Instance` as a temporary fallback, and
never caches the real scheduler — so every background-thread call runs
on the caller's thread.
## Fix
Change the factory to use `Application.Current?.Dispatcher`, which
returns the **WPF Application's UI dispatcher** (not the calling
thread's dispatcher):
```csharp
// After
public static IScheduler WpfMainThreadScheduler { get; } = new WaitForDispatcherScheduler(
static () =>
{
var dispatcher = Application.Current?.Dispatcher
?? throw new InvalidOperationException("WPF Application has not been initialized yet.");
return new DispatcherScheduler(dispatcher);
});
```
**Behaviour:**
- Before `Application` is created (very early in startup): factory
throws `InvalidOperationException` → caught → retried on next
`Schedule()` call (same safe behaviour as before)
- Once `Application.Current` is set (from **any** thread, including
background): factory succeeds → `_scheduler` cached as
`DispatcherScheduler` bound to the UI dispatcher
- All subsequent `Schedule()` calls →
`DispatcherScheduler.BeginInvoke()` → work correctly marshalled to UI
thread
## Changes
- **`src/ReactiveUI.Wpf/Builder/WpfReactiveUIBuilderExtensions.cs`** —
fix the factory lambda
- **`src/tests/ReactiveUI.Tests/WaitForDispatcherSchedulerTests.cs`** —
add regression test
`FactoryThrowsThenSucceeds_CachesSuccessfulScheduler` covering the
retry-then-cache path
All 18 `WaitForDispatcherSchedulerTests` pass on net8.0, net9.0, and
net10.0.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1f35503 commit fed5931
3 files changed
Lines changed: 66 additions & 8 deletions
File tree
- src
- ReactiveUI.Wpf/Builder
- ReactiveUI/Interactions
- tests/ReactiveUI.Tests
Lines changed: 9 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| |||
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
21 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
22 | 30 | | |
23 | 31 | | |
24 | 32 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
| 121 | + | |
127 | 122 | | |
128 | 123 | | |
129 | 124 | | |
| |||
Lines changed: 56 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
70 | 125 | | |
71 | 126 | | |
72 | 127 | | |
| |||
0 commit comments