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

  1. Capture the scheduler (or dispatcher) once on the UI thread and pass it to background code.
  2. Marshal back to the UI thread before accessing Current (e.g. Dispatcher.RunAsync from a stored dispatcher).
  3. Use Window.Current.Dispatcher / the dispatcher captured in App startup instead of re-resolving per call.
  4. 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

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


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)