{"record":{"id":"1be9b5d6cbe85f43","repo":"dotnet/reactive","slug":"scheduler-dispatcherobservable","errorCode":null,"errorMessage":"scheduler","messagePattern":"scheduler","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Linq/DispatcherObservable.cs","lineNumber":82,"sourceCode":"\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher scheduler.\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=\"scheduler\">Dispatcher scheduler to notify observers on.</param>\n        /// <returns>The source sequence whose observations happen on the specified dispatcher scheduler.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"scheduler\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherScheduler scheduler)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (scheduler == null)\n            {\n                throw new ArgumentNullException(nameof(scheduler));\n            }\n\n            return ObserveOn_(source, scheduler.Dispatcher, scheduler.Priority);\n        }\n\n\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.\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=\"dispatcherObject\">Object to get the dispatcher from.</param>\n        /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"dispatcherObject\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject)\n        {\n            if (source == null)\n            {","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Linq/DispatcherObservable.cs#L64-L100","documentation":"Thrown by ObserveOn(source, DispatcherScheduler) when the DispatcherScheduler argument is null. The operator reads scheduler.Dispatcher to obtain the target dispatcher; a null scheduler gives no scheduling target, so it fails fast with ArgumentNullException('scheduler').","triggerScenarios":"Passing a DispatcherScheduler variable that was never constructed, or one obtained from a factory/property that returned null when no dispatcher exists on the current thread.","commonSituations":"Using DispatcherScheduler.Current on a background thread without a dispatcher and caching a null result, or DI containers failing to resolve the scheduler instance.","solutions":["Construct the scheduler explicitly: new DispatcherScheduler(dispatcher)","Use Application.Current.Dispatcher to build the scheduler instead of relying on a null-propagating getter","Guard the scheduler with a null check and fall back to a non-dispatcher scheduler"],"exampleFix":"// before\nDispatcherScheduler sched = DispatcherScheduler.Current; // null off the UI thread\nsource.ObserveOn(sched);\n// after\nvar sched = new DispatcherScheduler(Application.Current.Dispatcher);\nsource.ObserveOn(sched);","handlingStrategy":"validation","validationCode":"var scheduler = sched ?? new DispatcherScheduler(Application.Current.Dispatcher);","typeGuard":"bool IsValidScheduler(DispatcherScheduler s) => s is not null && s.Dispatcher is not null;","tryCatchPattern":"try { stream.ObserveOn(scheduler).Subscribe(...); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"scheduler\") { scheduler = new DispatcherScheduler(Application.Current.Dispatcher); }","preventionTips":["Avoid caching DispatcherScheduler.Current results from background threads","Resolve schedulers through DI with a guaranteed registration","Assert scheduler non-null in view-model constructors"],"tags":["csharp","null-argument","rx","wpf","scheduler"],"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"}