AvaloniaUI/Avalonia · error · InvalidOperationException

IterationCount value cannot be larger than long.MaxValue.

Error message

IterationCount value cannot be larger than long.MaxValue.

What it means

Thrown when IterationCount.RepeatType is IterationType.Many and IterationCount.Value (a ulong) exceeds long.MaxValue. AnimationInstance internally stores the count as a long?, so any value above long.MaxValue would overflow the cast and is rejected up front.

Source

Thrown at src/Avalonia.Base/Animation/AnimationInstance`1.cs:106

            if (_speedRatio < 0d)
                throw new InvalidOperationException("SpeedRatio value cannot be negative.");

            _initialDelay = _animation.Delay;
            if (_initialDelay < TimeSpan.Zero)
                throw new InvalidOperationException("Delay value cannot be negative.");

            _duration = _animation.Duration;
            if (_duration < TimeSpan.Zero)
                throw new InvalidOperationException("Duration value cannot be negative.");

            _iterationDelay = _animation.DelayBetweenIterations;
            if (_iterationDelay < TimeSpan.Zero)
                throw new InvalidOperationException("DelayBetweenIterations value cannot be negative.");

            if (_animation.IterationCount.RepeatType == IterationType.Many)
            {
                if (_animation.IterationCount.Value > long.MaxValue)
                    throw new InvalidOperationException("IterationCount value cannot be larger than long.MaxValue.");
                _iterationCount = (long)_animation.IterationCount.Value;
            }
            else
            {
                _iterationCount = null;
            }

            _playbackDirectionPrev = _playbackDirection;
            _playbackDirection = _animation.PlaybackDirection;
            _fillMode = _animation.FillMode;
        }

        protected override void Unsubscribed()
        {
            // Guard against reentrancy: DoComplete() can trigger Unsubscribed() via the
            // _onCompleteAction disposal chain, and then PublishCompleted() calls it again.
            var timerSub = _timerSub;
            _timerSub = null;

View on GitHub (pinned to 11c5427268)

Solutions

  1. Use IterationCount.Infinite (IterationType.Infinite) instead of an astronomically large number when you mean 'forever'.
  2. Cap the parsed value at long.MaxValue before constructing the IterationCount.
  3. Validate user-supplied iteration counts against long.MaxValue before assigning.

Example fix

// before
animation.IterationCount = new IterationCount(ulong.MaxValue);

// after
animation.IterationCount = IterationCount.Infinite;
Defensive patterns

Strategy: validation

Validate before calling

const ulong maxLong = (ulong)long.MaxValue;
if (ic.RepeatType == IterationType.Many && ic.Value > maxLong)
    ic = IterationCount.Infinite;

Prevention

When it happens

Trigger: Calling Animation.Play with an IterationCount constructed from a ulong greater than long.MaxValue (9223372036854775807) and RepeatType.Many.

Common situations: Parsing a huge iteration count from input; using 'Infinite' semantics incorrectly by passing an enormous number instead of IterationType.Infinite.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/9fcc6c4c613d2cdc. Report an issue: GitHub.