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
- Use IterationCount.Infinite (IterationType.Infinite) instead of an astronomically large number when you mean 'forever'.
- Cap the parsed value at long.MaxValue before constructing the IterationCount.
- 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
- Use IterationCount.Infinite for unbounded repetition instead of huge numbers.
- Validate parsed iteration counts against long.MaxValue before constructing.
- Treat 'forever' semantics explicitly rather than approximating with large numbers.
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
- Duration value cannot be negative.
- DelayBetweenIterations value cannot be negative.
- This cue object's value should be within or equal to 0.0 and
- Invalid value
- IterationCount can't be a negative number.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/9fcc6c4c613d2cdc.
Report an issue: GitHub.