{"record":{"id":"46b1b9c245d36220","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-control","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/Platforms/Desktop/Concurrency/ControlScheduler.cs","lineNumber":28,"sourceCode":"{\n    /// <summary>\n    /// Represents an object that schedules units of work on the message loop associated with a Windows Forms control.\n    /// </summary>\n    public class ControlScheduler : LocalScheduler, ISchedulerPeriodic\n    {\n        private readonly Control _control;\n\n        /// <summary>\n        /// Constructs a ControlScheduler that schedules units of work on the message loop associated with the specified Windows Forms control.\n        /// </summary>\n        /// <param name=\"control\">Windows Forms control to get the message loop from.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"control\"/> is null.</exception>\n        /// <remarks>\n        /// This scheduler type is typically used indirectly through the <see cref=\"Linq.ControlObservable.ObserveOn{TSource}\"/> and <see cref=\"Linq.ControlObservable.SubscribeOn{TSource}\"/> method overloads that take a Windows Forms control.\n        /// </remarks>\n        public ControlScheduler(Control control)\n        {\n            _control = control ?? throw new ArgumentNullException(nameof(control));\n        }\n\n        /// <summary>\n        /// Gets the control associated with the ControlScheduler.\n        /// </summary>\n        public Control Control => _control;\n\n        /// <summary>\n        /// Schedules an action to be executed on the message loop associated with the control.\n        /// </summary>\n        /// <typeparam name=\"TState\">The type of the state passed to the scheduled action.</typeparam>\n        /// <param name=\"state\">State passed to the action to be executed.</param>\n        /// <param name=\"action\">Action to be executed.</param>\n        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"action\"/> is null.</exception>\n        public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)\n        {\n            if (action == null)","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Concurrency/ControlScheduler.cs#L10-L46","documentation":"The ControlScheduler constructor stores the Windows Forms Control on which work will be invoked via control.BeginInvoke, so a null control is rejected immediately with ArgumentNullException. ControlScheduler cannot function without a control because it uses the control's handle/thread to marshal execution.","triggerScenarios":"new ControlScheduler(null), or passing a Control field/property that is null (e.g. a form control accessed before InitializeComponent or from a context where the control reference was never assigned).","commonSituations":"Constructor injection where the WinForms control dependency was not registered; using this.SomeControl in a control whose designer field is not yet initialized; unit tests constructing the scheduler without a real control.","solutions":["Pass a valid, constructed Control instance (e.g. the form or a control on it).","Check for null before constructing: if (control == null) throw/log instead.","Ensure InitializeComponent has run so designer control fields are assigned before scheduler creation."],"exampleFix":"// before\nvar scheduler = new ControlScheduler(_myControl); // _myControl null before InitializeComponent\n// after\nInitializeComponent();\nif (_myControl == null) throw new InvalidOperationException(\"control not initialized\");\nvar scheduler = new ControlScheduler(_myControl);","handlingStrategy":"validation","validationCode":"if (control == null || control.IsDisposed) throw new InvalidOperationException(\"A live WinForms control is required\");\nvar scheduler = new ControlScheduler(control);","typeGuard":"static bool HasControl(Control c) => c is not null && !c.IsDisposed;","tryCatchPattern":"try { var s = new ControlScheduler(control); } catch (ArgumentNullException ex) when (ex.ParamName == \"control\") { /* log missing control dependency */ }","preventionTips":["Construct schedulers after InitializeComponent so control fields exist","Assert control dependencies at injection time","Prefer ObserveOn(control)/SubscribeOn(control) helpers over manual scheduler construction"],"tags":["dotnet","winforms","null-argument","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"}