dotnet/reactive · error · InvalidOperationException
No current Window object found to obtain a CoreDispatcher…
Error message
No current Window object found to obtain a CoreDispatcher from.
What it means
CoreDispatcherScheduler.Current throws InvalidOperationException('No current Window object found to obtain a CoreDispatcher from.') when CoreWindow.GetForCurrentThread() returns null. This property only works when called on a thread that has a CoreWindow (the UI thread); Rx surfaces a clear message instead of a NullReferenceException on window.Dispatcher.
Solutions
- Capture the scheduler (or dispatcher) once on the UI thread and pass it to background code.
- Marshal back to the UI thread before accessing Current (e.g. Dispatcher.RunAsync from a stored dispatcher).
- Use Window.Current.Dispatcher / the dispatcher captured in App startup instead of re-resolving per call.
- In tests, inject a fake IScheduler rather than using CoreDispatcherScheduler.Current.
Example fix
// before
public async Task LoadAsync() {
var data = await Fetch();
var sched = CoreDispatcherScheduler.Current; // throws off UI thread
}
// after
_scheduler = CoreDispatcherScheduler.Current; // captured on UI thread at startup
public async Task LoadAsync() {
var data = await Fetch();
_scheduler.Schedule(() => Render(data));
} Defensive patterns
Strategy: fallback
Validate before calling
if (CoreWindow.GetForCurrentThread() == null)
throw new InvalidOperationException("CoreDispatcherScheduler.Current requires the UI thread"); Type guard
bool OnUiThread() => CoreWindow.GetForCurrentThread() is not null;
Try / catch
try { var sched = CoreDispatcherScheduler.Current; } catch (InvalidOperationException ex) when (ex.Message.Contains("No current Window")) { /* use dispatcher captured at startup */ } Prevention
- Capture CoreDispatcherScheduler.Current once at app startup and inject it
- Never access Current inside Task.Run or after await without resuming on the UI thread
- Inject IScheduler in tests instead of relying on Current
- Use Context Synchronization (TaskScheduler.FromCurrentSynchronizationContext) for UI marshaling
When it happens
Trigger: Accessing CoreDispatcherScheduler.Current from a background/threadpool thread, from a Task.Run body, an event raised on a non-UI thread, or in unit tests without a CoreWindow.
Common situations: UWP apps calling Current inside async data-loading code that resumed on the thread pool; failing to use the DispatcherQueue/CoreDispatcher captured at startup; running scheduler-dependent code in unit tests.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- action
- dispatcher
- action (Parameter 'action')
- ArgumentNullException: dispatcher
- ArgumentNullException: source
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/dc2257858d60d23a.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Concurrency/CoreDispatcherScheduler.cs:54
/// </summary>
/// <param name="dispatcher">Dispatcher to schedule work on.</param>
/// <param name="priority">Priority for scheduled units of work.</param>
/// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is <c>null</c>.</exception>
public CoreDispatcherScheduler(CoreDispatcher dispatcher, CoreDispatcherPriority priority)
{
Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
Priority = priority;
}
/// <summary>
/// Gets the scheduler that schedules work on the <see cref="CoreDispatcher"/> associated with the current Window.
/// </summary>
public static CoreDispatcherScheduler Current
{
get
{
var window = CoreWindow.GetForCurrentThread()
?? throw new InvalidOperationException(Strings_WindowsThreading.NO_WINDOW_CURRENT);
return new CoreDispatcherScheduler(window.Dispatcher);
}
}
/// <summary>
/// Gets the <see cref="CoreDispatcher"/> associated with the <see cref="CoreDispatcherScheduler"/>.
/// </summary>
public CoreDispatcher Dispatcher { get; }
private DispatcherQueue? _dispatcherQueue;
/// <summary>
/// Gets the priority at which work is scheduled.
/// </summary>
public CoreDispatcherPriority Priority { get; }
/// <summary>
/// Schedules an action to be executed on the dispatcher.View on GitHub (pinned to 94b5d5ab91)