dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'control')
Error message
Value cannot be null. (Parameter 'control')
What it means
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.
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.
Example fix
// before
source.SubscribeOn(_panel); // _panel is null
// after
if (_panel != null) { source.SubscribeOn(_panel).Subscribe(...); } Defensive patterns
Strategy: validation
Validate before calling
if (control is null || control.IsDisposed) return; source.SubscribeOn(control);
Type guard
bool IsUsableControl(System.Windows.Forms.Control c) => c is not null && !c.IsDisposed && c.IsHandleCreated;
Try / catch
try { source.SubscribeOn(control).Subscribe(obs); } catch (ArgumentNullException ex) when (ex.ParamName == "control") { Log("Target control was null"); } Prevention
- Subscribe only after InitializeComponent has run
- Hold control references in fields initialized in the constructor/designer
- Check control lifetime (IsDisposed) as well as null
When it happens
Trigger: Calling source.SubscribeOn(null), often with a control field not yet assigned (e.g., called before InitializeComponent or after Dispose set it to null).
Common situations: 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.
Related errors
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'action')
- Value cannot be null. (Parameter 'source')
- nameof(onError)
- nameof(onCompleted)
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/e6e956d842d81113.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive.Windows.Forms/System.Reactive.Linq/ControlObservable.cs:40
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="control">Windows Forms control whose associated message loop is used to perform subscription and unsubscription actions on.</param>
/// <returns>The source sequence whose subscriptions and unsubscriptions happen on the Windows Forms message loop associated with the specified control.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="control"/> is null.</exception>
/// <remarks>
/// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified control.
/// In order to invoke observer callbacks on the specified control, e.g. to render results in a control, use <see cref="ObserveOn"/>.
/// </remarks>
public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, Control control)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (control == null)
{
throw new ArgumentNullException(nameof(control));
}
return Synchronization.SubscribeOn(source, new ControlScheduler(control));
}
/// <summary>
/// Wraps the source sequence in order to run its observer callbacks on the Windows Forms message loop associated with the specified control.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="control">Windows Forms control whose associated message loop is used to notify observers on.</param>
/// <returns>The source sequence whose observations happen on the Windows Forms message loop associated with the specified control.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="control"/> is null.</exception>
public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Control control)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));View on GitHub (pinned to 94b5d5ab91)