dotnet/wpf · error · ArgumentOutOfRangeException

SR.Timing_RepeatBehaviorInvalidIterationCount

Error message

SR.Timing_RepeatBehaviorInvalidIterationCount

What it means

The RepeatBehavior(double count) constructor requires a finite, non-negative iteration count. Passing infinity, NaN, or a negative number throws ArgumentOutOfRangeException with the message Timing_RepeatBehaviorInvalidIterationCount. The constructor is strict because iteration count drives the timing engine and undefined values would corrupt the animation schedule.

Solutions

  1. Validate count is finite and >= 0 before constructing: double.IsFinite(count) && count >= 0.
  2. For infinite repetition use RepeatBehavior.Forever instead of double.PositiveInfinity.
  3. Clamp or default invalid computed values, e.g. Math.Max(1, count).

Example fix

// before
var rb = new RepeatBehavior(-1); // meant 'forever'
// after
var rb = RepeatBehavior.Forever;
Defensive patterns

Strategy: validation

Validate before calling

if (!double.IsFinite(count) || count < 0) throw new ArgumentException($"Invalid repeat count: {count}");
var rb = new RepeatBehavior(count);

Type guard

static bool IsValidRepeatCount(double count) => double.IsFinite(count) && count >= 0;

Try / catch

RepeatBehavior rb;
try { rb = new RepeatBehavior(count); }
catch (ArgumentOutOfRangeException) { rb = RepeatBehavior.Forever; }

Prevention

When it happens

Trigger: new RepeatBehavior(double.PositiveInfinity), new RepeatBehavior(double.NaN), or new RepeatBehavior(-1) — any count that is Infinity, NaN, or < 0.

Common situations: Computing a repeat count from division that yields NaN (e.g., 0/0) or Infinity; a config/default value of -1 meaning 'infinite' from another API (use RepeatBehavior.Forever instead); parsing user input without sanitization.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/12dc297597c48163. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/RepeatBehavior.cs:39

    /// <para>A Forever RepeatBehavior specifies that a Timeline will repeat forever.</para>
    /// </summary>
    [TypeConverter(typeof(RepeatBehaviorConverter))]
    public readonly struct RepeatBehavior : IFormattable
    {
        private readonly double _iterationCount;
        private readonly TimeSpan _repeatDuration;
        private readonly RepeatBehaviorType _type;

        #region Constructors

        /// <summary>
        /// Creates a new RepeatBehavior that represents and iteration count.
        /// </summary>
        /// <param name="count">The number of iterations specified by this RepeatBehavior.</param>
        public RepeatBehavior(double count)
        {
            if (double.IsInfinity(count) || double.IsNaN(count) || count < 0.0)
                throw new ArgumentOutOfRangeException(nameof(count), SR.Format(SR.Timing_RepeatBehaviorInvalidIterationCount, count));

            _repeatDuration = TimeSpan.Zero;
            _iterationCount = count;
            _type = RepeatBehaviorType.IterationCount;
        }

        /// <summary>
        /// Creates a new RepeatBehavior that represents a repeat duration for which a Timeline will repeat
        /// its simple duration.
        /// </summary>
        /// <param name="duration">A TimeSpan representing the repeat duration specified by this RepeatBehavior.</param>
        public RepeatBehavior(TimeSpan duration)
        {
            if (duration < TimeSpan.Zero)
                throw new ArgumentOutOfRangeException(nameof(duration), SR.Format(SR.Timing_RepeatBehaviorInvalidRepeatDuration, duration));

            _iterationCount = 0.0;
            _repeatDuration = duration;

View on GitHub (pinned to 81131a70a4)