{"record":{"id":"c22c0393fa321b8b","repo":"dotnet/reactive","slug":"dispatcher-parameter-dispatcher","errorCode":null,"errorMessage":"dispatcher (Parameter 'dispatcher')","messagePattern":"dispatcher \\(Parameter 'dispatcher'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/CoreDispatcherObservable.cs","lineNumber":40,"sourceCode":"\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"dispatcher\">Dispatcher whose associated message loop is used to notify observers on.</param>\n        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"dispatcher\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, CoreDispatcher dispatcher)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (dispatcher == null)\n            {\n                throw new ArgumentNullException(nameof(dispatcher));\n            }\n\n            return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dispatcher));\n        }\n\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"dispatcher\">Dispatcher whose associated message loop is used to notify observers on.</param>\n        /// <param name=\"priority\">Priority to schedule work items at.</param>\n        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"dispatcher\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, CoreDispatcher dispatcher, CoreDispatcherPriority priority)\n        {\n            if (source == null)\n            {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/CoreDispatcherObservable.cs#L22-L58","documentation":"ObserveOn needs a CoreDispatcher to schedule notifications on the UI thread; a null dispatcher cannot be used to create the CoreDispatcherScheduler. ArgumentNullException with ParamName 'dispatcher' is thrown by the second guard. The dispatcher is required at call time, not at subscription time.","triggerScenarios":"Calling source.ObserveOn(dispatcher) where the CoreDispatcher came from a Window/CoreWindow that is null (e.g. code running on a background thread without a window, or CoreWindow.Active captured before activation).","commonSituations":"Calling from a background thread where CoreWindow.GetCurrentView() throws or returns null; caching a dispatcher in a static field before the window exists; app shutdown after the window is closed.","solutions":["Obtain the dispatcher on the UI thread (e.g. CoreApplication.MainView.CoreWindow.Dispatcher) and pass it after ensuring it is non-null.","Capture the dispatcher during app startup (OnLaunched) and reuse that cached value.","Add a guard: if (dispatcher == null) throw/log before calling ObserveOn, or fall back to the current scheduler."],"exampleFix":"// before\nvar d = CoreWindow.GetForCurrentThread()?.Dispatcher;\nstream.ObserveOn(d).Subscribe(...); // d may be null off the UI thread\n// after\nvar d = CoreApplication.MainView.CoreWindow.Dispatcher;\nif (d != null) stream.ObserveOn(d).Subscribe(...);","handlingStrategy":"validation","validationCode":"var dispatcher = CoreApplication.MainView.CoreWindow?.Dispatcher;\nif (dispatcher == null) { /* defer or log; do not call ObserveOn */ }","typeGuard":"bool HasDispatcher(CoreDispatcher d) => d is not null;","tryCatchPattern":"try { stream.ObserveOn(dispatcher).Subscribe(onNext); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"dispatcher\") { /* subscribe without marshalling or retry on UI thread */ }","preventionTips":["Capture the dispatcher on the UI thread during OnLaunched and cache it.","Never call CoreWindow.GetForCurrentThread() from background threads.","Check dispatcher for null before any ObserveOn call."],"tags":["argument-null","winrt","dispatcher","ui-thread"],"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"}