{"record":{"id":"0e06a3d63e48f511","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-dispatcher","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'dispatcher')","messagePattern":"Value cannot be null\\. \\(Parameter 'dispatcher'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Concurrency/DispatcherScheduler.cs","lineNumber":45,"sourceCode":"        /// </summary>\n        public static DispatcherScheduler Current\n        {\n            get\n            {\n                var dispatcher = System.Windows.Threading.Dispatcher.FromThread(Thread.CurrentThread)\n                    ?? throw new InvalidOperationException(Strings_WindowsThreading.NO_DISPATCHER_CURRENT_THREAD);\n                return new DispatcherScheduler(dispatcher);\n            }\n        }\n\n        /// <summary>\n        /// Constructs a <see cref=\"DispatcherScheduler\"/> that schedules units of work on the given <see cref=\"System.Windows.Threading.Dispatcher\"/>.\n        /// </summary>\n        /// <param name=\"dispatcher\"><see cref=\"DispatcherScheduler\"/> to schedule work on.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"dispatcher\"/> is <c>null</c>.</exception>\n        public DispatcherScheduler(System.Windows.Threading.Dispatcher dispatcher)\n        {\n            Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));\n            Priority = System.Windows.Threading.DispatcherPriority.Normal;\n\n        }\n\n        /// <summary>\n        /// Constructs a <see cref=\"DispatcherScheduler\"/> that schedules units of work on the given <see cref=\"System.Windows.Threading.Dispatcher\"/> at the given priority.\n        /// </summary>\n        /// <param name=\"dispatcher\"><see cref=\"DispatcherScheduler\"/> to schedule work on.</param>\n        /// <param name=\"priority\">Priority at which units of work are scheduled.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"dispatcher\"/> is <c>null</c>.</exception>\n        public DispatcherScheduler(System.Windows.Threading.Dispatcher dispatcher, System.Windows.Threading.DispatcherPriority priority)\n        {\n            Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));\n            Priority = priority;\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"System.Windows.Threading.Dispatcher\"/> associated with the <see cref=\"DispatcherScheduler\"/>.","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Concurrency/DispatcherScheduler.cs#L27-L63","documentation":"The DispatcherScheduler(System.Windows.Threading.Dispatcher) constructor requires a non-null dispatcher because all work is queued through it. Passing null throws ArgumentNullException with Parameter 'dispatcher'.","triggerScenarios":"new DispatcherScheduler(null), or passing a dispatcher obtained from Dispatcher.FromThread(t) where thread t has no Dispatcher, or Application.Current.Dispatcher when Application.Current is null (non-WPF context).","commonSituations":"Resolving the dispatcher from an arbitrary worker thread whose FromThread result is null; calling Application.Current.Dispatcher in unit tests or a WPF-less host; dependency-injection registrations that yield a null dispatcher.","solutions":["Pass a live dispatcher such as Application.Current.Dispatcher or Dispatcher.CurrentDispatcher (on the UI thread).","If you have a Thread, verify Dispatcher.FromThread(thread) is non-null before constructing.","Guard with ?? throw or a fallback scheduler when the dispatcher may be unavailable."],"exampleFix":"// before\nvar dispatcher = Dispatcher.FromThread(uiThread); // null if no dispatcher on that thread\nvar scheduler = new DispatcherScheduler(dispatcher);\n// after\nvar dispatcher = Application.Current?.Dispatcher ?? Dispatcher.CurrentDispatcher;\nvar scheduler = new DispatcherScheduler(dispatcher);","handlingStrategy":"validation","validationCode":"var dispatcher = Application.Current?.Dispatcher ?? System.Windows.Threading.Dispatcher.CurrentDispatcher;\nif (dispatcher == null) { /* fallback */ }\nvar scheduler = new DispatcherScheduler(dispatcher);","typeGuard":"static bool HasLiveDispatcher(System.Windows.Threading.Dispatcher d) => d is not null;","tryCatchPattern":"try { var s = new DispatcherScheduler(dispatcher); } catch (ArgumentNullException ex) when (ex.ParamName == \"dispatcher\") { /* use Scheduler.Default */ }","preventionTips":["Resolve dispatchers on the UI thread, not worker threads","Check Application.Current for null in non-WPF hosts and tests","Cache the dispatcher reference instead of re-fetching per call"],"tags":["dotnet","wpf","null-argument","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"}