{"record":{"id":"658baefeaba6313f","repo":"dotnet/reactive","slug":"dispatcher","errorCode":null,"errorMessage":"dispatcher","messagePattern":"dispatcher","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Linq/DispatcherObservable.cs","lineNumber":35,"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, Dispatcher 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 ObserveOn_(source, 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, Dispatcher dispatcher, DispatcherPriority priority)\n        {\n            if (source == null)\n            {","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Linq/DispatcherObservable.cs#L17-L53","documentation":"The second validation in DispatcherObservable.ObserveOn throws ArgumentNullException with parameter name 'dispatcher' when the WPF Dispatcher is null. The dispatcher is required to post observer callbacks to the UI thread.","triggerScenarios":"Calling source.ObserveOn(null) — e.g. Dispatcher.CurrentDispatcher captured before the UI thread existed, Application.Current null in tests, or a control's Dispatcher accessed after the control was disposed.","commonSituations":"Unit tests without a WPF application context (Application.Current is null); background threads that never created a dispatcher; shutdown races where the window's Dispatcher is already gone.","solutions":["Pass a valid non-null Dispatcher, e.g. Application.Current.Dispatcher or the window's Dispatcher.","Capture the dispatcher on the UI thread at startup and store it, rather than resolving lazily.","In tests, provide a dispatcher via a DispatcherSynchronizationContext or a test scheduler instead of null."],"exampleFix":"// before\nsource.ObserveOn(Application.Current.Dispatcher).Subscribe(OnNext); // Application.Current is null in tests\n// after\nvar dispatcher = Application.Current?.Dispatcher ?? _uiDispatcher;\nsource.ObserveOn(dispatcher).Subscribe(OnNext);","handlingStrategy":"validation","validationCode":"var dispatcher = Application.Current?.Dispatcher\n                 ?? (System.Windows.Threading.Dispatcher.FromThread(Thread.CurrentThread)\n                     ?? Dispatcher.CurrentDispatcher);\nif (dispatcher == null) throw new InvalidOperationException(\"No WPF dispatcher available\");\nsource.ObserveOn(dispatcher).Subscribe(OnNext);","typeGuard":"static bool HasDispatcher(Dispatcher d) => d is not null && !d.HasShutdownStarted;","tryCatchPattern":"try { source.ObserveOn(dispatcher).Subscribe(OnNext); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"dispatcher\") { log.Error(\"Dispatcher was null at ObserveOn\"); }","preventionTips":["Capture the UI Dispatcher at application startup and store it statically","In unit tests use a test scheduler or spin up a DispatcherFrame","Check Dispatcher.HasShutdownStarted before marshaling during app exit"],"tags":["argument-null","wpf","rxjs","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"}