{"record":{"id":"2c67cc9a03308d4a","repo":"dotnet/reactive","slug":"dispatcher-coredispatcherscheduler","errorCode":null,"errorMessage":"dispatcher","messagePattern":"dispatcher","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Concurrency/CoreDispatcherScheduler.cs","lineNumber":30,"sourceCode":"namespace System.Reactive.Concurrency\n{\n    /// <summary>\n    /// Represents an object that schedules units of work on a <see cref=\"CoreDispatcher\"/>.\n    /// </summary>\n    /// <remarks>\n    /// This scheduler type is typically used indirectly through the <see cref=\"Linq.CoreDispatcherObservable.ObserveOnCoreDispatcher{TSource}(IObservable{TSource})\"/> and <see cref=\"Linq.CoreDispatcherObservable.SubscribeOnCoreDispatcher{TSource}(IObservable{TSource})\"/> methods that use the current CoreDispatcher.\n    /// </remarks>\n    [CLSCompliant(false)]\n    public sealed class CoreDispatcherScheduler : LocalScheduler, ISchedulerPeriodic\n    {\n        /// <summary>\n        /// Constructs a <see cref=\"CoreDispatcherScheduler\"/> that schedules units of work on the given <see cref=\"CoreDispatcher\"/>.\n        /// </summary>\n        /// <param name=\"dispatcher\">Dispatcher to schedule work on.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"dispatcher\"/> is <c>null</c>.</exception>\n        public CoreDispatcherScheduler(CoreDispatcher dispatcher)\n        {\n            Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));\n            Priority = CoreDispatcherPriority.Normal;           \n        }\n\n        /// <summary>\n        /// Constructs a <see cref=\"CoreDispatcherScheduler\"/> that schedules units of work on the given <see cref=\"CoreDispatcher\"/> with the given priority.\n        /// </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>","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Concurrency/CoreDispatcherScheduler.cs#L12-L48","documentation":"The CoreDispatcherScheduler constructor throws ArgumentNullException when the passed CoreDispatcher is null. A scheduler without a dispatcher has nothing to schedule work on, so Rx rejects it at construction time rather than failing later on the first Schedule call.","triggerScenarios":"new CoreDispatcherScheduler(null), commonly when the dispatcher was obtained from CoreWindow.GetForCurrentThread()?.Dispatcher or a Window whose dispatcher is unavailable off the UI thread.","commonSituations":"UWP apps capturing a dispatcher in a ViewModel that is constructed on a background thread, or storing Window/CoreWindow in a static that resolves to null at scheduler creation time.","solutions":["Obtain the dispatcher on the UI thread: CoreWindow.GetForCurrentThread().Dispatcher.","Use CoreDispatcherScheduler.Current instead of manually passing a dispatcher.","If the dispatcher may be null, defer scheduler creation until the UI context is available (e.g. via CoreApplication.Run or Window.Dispatcher inside the view)."],"exampleFix":"// before\nvar sched = new CoreDispatcherScheduler(CoreWindow.GetForCurrentThread()?.Dispatcher); // may be null\n// after\nvar window = CoreWindow.GetForCurrentThread();\nif (window == null) throw new InvalidOperationException(\"Must be called on UI thread\");\nvar sched = new CoreDispatcherScheduler(window.Dispatcher);","handlingStrategy":"type-guard","validationCode":"var dispatcher = CoreWindow.GetForCurrentThread()?.Dispatcher;\nif (dispatcher == null) throw new InvalidOperationException(\"must construct on UI thread\");\nvar sched = new CoreDispatcherScheduler(dispatcher);","typeGuard":"bool HasDispatcher(CoreWindow w) => w?.Dispatcher is not null;","tryCatchPattern":"try { var sched = new CoreDispatcherScheduler(d); } catch (ArgumentNullException ex) when (ex.ParamName == \"dispatcher\") { /* defer to UI thread */ }","preventionTips":["Capture the dispatcher once on the UI thread at startup","Avoid ?.Dispatcher chains feeding straight into the constructor","Prefer CoreDispatcherScheduler.Current when on the UI thread"],"tags":["dotnet","rx","uwp","argument-null","dispatcher"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}