stride3d/stride · error · ArgumentOutOfRangeException

Expecting value >= 1

Error message

Expecting value >= 1

What it means

The same ProgressStatusEventArgs constructor requires stepCount >= 1 when step-based progress is used; passing 0 or a negative stepCount throws ArgumentOutOfRangeException('Expecting value >= 1'). A stepCount < 1 would make percentage computation (currentStep/stepCount) undefined.

Solutions

  1. Pass a positive total; skip the event when the work list is empty
  2. Compute stepCount as Math.Max(1, total) only when work exists, otherwise don't fire the event
  3. Use the indeterminate-progress path (no steps) instead of stepCount = 0

Example fix

// before
new ProgressStatusEventArgs("Loading", i, items.Count); // 0 when empty
// after
if (items.Count > 0)
    new ProgressStatusEventArgs("Loading", i, items.Count);
Defensive patterns

Strategy: validation

Validate before calling

if (items.Count > 0)
    OnProgress(new ProgressStatusEventArgs(message, i, items.Count));

Type guard

bool isValidStepCount(int total) => total >= 1;

Try / catch

try { return new ProgressStatusEventArgs(msg, current, total); }
catch (ArgumentOutOfRangeException) { return new ProgressStatusEventArgs(msg, current, Math.Max(1, total)); }

Prevention

When it happens

Trigger: new ProgressStatusEventArgs(msg, current, 0) — e.g. total computed from an empty collection's Count, or a division/parse result of zero.

Common situations: Progress over an empty work list (total = items.Count == 0); config-driven total that defaults to 0; using 0 to mean 'indeterminate' — that is only valid for stepCount when it stays >= 1 or via the message-only path.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/e76529382f7b019d. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core/Diagnostics/ProgressStatusEventArgs.cs:32

    public ProgressStatusEventArgs(string message)
    {
        ArgumentNullException.ThrowIfNull(message);
        Message = message;
        StepCount = 1;
        HasKnownSteps = false;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ProgressStatusEventArgs"/> class.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="currentStep">The current step.</param>
    /// <param name="stepCount">The step count.</param>
    public ProgressStatusEventArgs(string message, int currentStep, int stepCount)
    {
        ArgumentNullException.ThrowIfNull(message);
        if (currentStep < 0) throw new ArgumentOutOfRangeException(nameof(currentStep), "Expecting value >= 0");
        if (stepCount < 1) throw new ArgumentOutOfRangeException(nameof(stepCount), "Expecting value >= 1");
        Message = message;
        CurrentStep = currentStep;
        StepCount = stepCount;
        HasKnownSteps = true;
    }

    /// <summary>
    /// Gets or sets the message associated with the progress.
    /// </summary>
    /// <value>The message.</value>
    public string Message { get; }

    /// <summary>
    /// Gets or sets a value indicating whether this instance has known steps (<see cref="CurrentStep"/> and 
    /// <see cref="StepCount"/> are valid).
    /// </summary>
    /// <value><c>true</c> if this instance has known steps; otherwise, <c>false</c>.</value>
    public bool HasKnownSteps { get; }

View on GitHub (pinned to 96fad776d2)