{"record":{"id":"dc2257858d60d23a","repo":"dotnet/reactive","slug":"no-current-window-object-found-to-obtain-a-coredispatcher","errorCode":null,"errorMessage":"No current Window object found to obtain a CoreDispatcher from.","messagePattern":"No current Window object found to obtain a CoreDispatcher from\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Concurrency/CoreDispatcherScheduler.cs","lineNumber":54,"sourceCode":"        /// </summary>\n        /// <param name=\"dispatcher\">Dispatcher to schedule work on.</param>\n        /// <param name=\"priority\">Priority for scheduled units of work.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"dispatcher\"/> is <c>null</c>.</exception>\n        public CoreDispatcherScheduler(CoreDispatcher dispatcher, CoreDispatcherPriority priority)\n        {\n            Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));\n            Priority = priority;\n        }\n\n        /// <summary>\n        /// Gets the scheduler that schedules work on the <see cref=\"CoreDispatcher\"/> associated with the current Window.\n        /// </summary>\n        public static CoreDispatcherScheduler Current\n        {\n            get\n            {\n                var window = CoreWindow.GetForCurrentThread()\n                    ?? throw new InvalidOperationException(Strings_WindowsThreading.NO_WINDOW_CURRENT);\n                return new CoreDispatcherScheduler(window.Dispatcher);\n            }\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"CoreDispatcher\"/> associated with the <see cref=\"CoreDispatcherScheduler\"/>.\n        /// </summary>\n        public CoreDispatcher Dispatcher { get; }\n\n        private DispatcherQueue? _dispatcherQueue;\n\n        /// <summary>\n        /// Gets the priority at which work is scheduled.\n        /// </summary>\n        public CoreDispatcherPriority Priority { get; }\n\n        /// <summary>\n        /// Schedules an action to be executed on the dispatcher.","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Concurrency/CoreDispatcherScheduler.cs#L36-L72","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic async Task LoadAsync() {\n  var data = await Fetch();\n  var sched = CoreDispatcherScheduler.Current; // throws off UI thread\n}\n// after\n_scheduler = CoreDispatcherScheduler.Current; // captured on UI thread at startup\npublic async Task LoadAsync() {\n  var data = await Fetch();\n  _scheduler.Schedule(() => Render(data));\n}","handlingStrategy":"fallback","validationCode":"if (CoreWindow.GetForCurrentThread() == null)\n    throw new InvalidOperationException(\"CoreDispatcherScheduler.Current requires the UI thread\");","typeGuard":"bool OnUiThread() => CoreWindow.GetForCurrentThread() is not null;","tryCatchPattern":"try { var sched = CoreDispatcherScheduler.Current; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"No current Window\")) { /* use dispatcher captured at startup */ }","preventionTips":["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"],"tags":["dotnet","rx","uwp","threading","ui-thread"],"backgroundTag":"invalid-state-transition","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}