{"record":{"id":"e6e956d842d81113","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-control-controlobservable","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'control')","messagePattern":"Value cannot be null\\. \\(Parameter 'control'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive.Windows.Forms/System.Reactive.Linq/ControlObservable.cs","lineNumber":40,"sourceCode":"        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"control\">Windows Forms control whose associated message loop is used to perform subscription and unsubscription actions on.</param>\n        /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the Windows Forms message loop associated with the specified control.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"control\"/> is null.</exception>\n        /// <remarks>\n        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified control.\n        /// In order to invoke observer callbacks on the specified control, e.g. to render results in a control, use <see cref=\"ObserveOn\"/>.\n        /// </remarks>\n        public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, Control control)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (control == null)\n            {\n                throw new ArgumentNullException(nameof(control));\n            }\n\n            return Synchronization.SubscribeOn(source, new ControlScheduler(control));\n        }\n\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the Windows Forms message loop associated with the specified control.\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=\"control\">Windows Forms control whose associated message loop is used to notify observers on.</param>\n        /// <returns>The source sequence whose observations happen on the Windows Forms message loop associated with the specified control.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"control\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Control control)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive.Windows.Forms/System.Reactive.Linq/ControlObservable.cs#L22-L58","documentation":"ControlObservable.SubscribeOn throws ArgumentNullException when the Control is null. The control is needed to construct a ControlScheduler that marshals subscription onto the WinForms message loop, so a null control cannot proceed.","triggerScenarios":"Calling source.SubscribeOn(null), often with a control field not yet assigned (e.g., called before InitializeComponent or after Dispose set it to null).","commonSituations":"ViewModel/code-behind wiring where the control reference is fetched by name or from a DI container that returned null; using the extension from a non-UI context with no control available.","solutions":["Pass a valid System.Windows.Forms.Control instance to SubscribeOn.","Verify the control field is initialized (after InitializeComponent) before subscribing.","If no control exists, use a different scheduler (e.g., a dispatcher/taskpool scheduler) rather than a null control."],"exampleFix":"// before\nsource.SubscribeOn(_panel); // _panel is null\n// after\nif (_panel != null) { source.SubscribeOn(_panel).Subscribe(...); }","handlingStrategy":"validation","validationCode":"if (control is null || control.IsDisposed) return;\nsource.SubscribeOn(control);","typeGuard":"bool IsUsableControl(System.Windows.Forms.Control c) => c is not null && !c.IsDisposed && c.IsHandleCreated;","tryCatchPattern":"try { source.SubscribeOn(control).Subscribe(obs); } catch (ArgumentNullException ex) when (ex.ParamName == \"control\") { Log(\"Target control was null\"); }","preventionTips":["Subscribe only after InitializeComponent has run","Hold control references in fields initialized in the constructor/designer","Check control lifetime (IsDisposed) as well as null"],"tags":["csharp","rx","argumentnull","winforms"],"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"}