dotnet/wpf · error · InvalidEnumArgumentException

SR.Enum_Invalid (TimeSeekOrigin)

Error message

SR.Enum_Invalid (TimeSeekOrigin)

What it means

MediaClock.Seek validates the timeSeekOrigin enum argument; only TimeSeekOrigin.BeginTime and TimeSeekOrigin.Duration are defined. Any other cast/garbage value reaches the default case and throws InvalidEnumArgumentException with SR.Enum_Invalid("TimeSeekOrigin").

Solutions

  1. Pass only TimeSeekOrigin.BeginTime or TimeSeekOrigin.Duration.
  2. Validate with Enum.IsDefined(typeof(TimeSeekOrigin), value) before casting from int.
  3. Default unknown values to TimeSeekOrigin.BeginTime.

Example fix

// before
clock.Seek(offset, (TimeSeekOrigin)savedOrigin);
// after
var origin = Enum.IsDefined(typeof(TimeSeekOrigin), savedOrigin) ? (TimeSeekOrigin)savedOrigin : TimeSeekOrigin.BeginTime;
clock.Seek(offset, origin);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(TimeSeekOrigin), origin)) origin = TimeSeekOrigin.BeginTime;
clock.Seek(offset, origin);

Type guard

static bool IsDefinedTimeSeekOrigin(int v) => Enum.IsDefined(typeof(TimeSeekOrigin), v);
static TimeSeekOrigin SafeOrigin(int v) => Enum.IsDefined(typeof(TimeSeekOrigin), v) ? (TimeSeekOrigin)v : TimeSeekOrigin.BeginTime;

Try / catch

try { clock.Seek(offset, origin); }
catch (InvalidEnumArgumentException ex) { /* log and retry with BeginTime */ clock.Seek(offset, TimeSeekOrigin.BeginTime); }

Prevention

When it happens

Trigger: Calling clock.Seek(offset, TimeSeekOrigin.Duration) is handled, but clock.Seek(offset, (TimeSeekOrigin)99) or an uninitialized enum variable (default value 0, which is not a defined member) throws.

Common situations: Persisting the origin as an int in settings and casting back without validation; interop/COM callers passing a bad integer; a default(TimeSeekOrigin) of 0 passed straight through.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Animation/TimeManager.cs:257

        /// from the end position has no effect.</p>
        /// </remarks>
        public void Seek(int offset, TimeSeekOrigin origin)
        {
            if (_timeState >= TimeState.Paused)
            {
                switch (origin)
                {
                    case TimeSeekOrigin.BeginTime:
                        // Use the offset as the new time;
                        break;

                    case TimeSeekOrigin.Duration:
                        // Undefined for the global clock
                        return;

                    default:
                        // Invalid enum argument
                        throw new InvalidEnumArgumentException(SR.Format(SR.Enum_Invalid, "TimeSeekOrigin"));
                }

                // Truncate to the beginning
                if (offset < 0)
                {
                    offset = 0;
                }

                TimeSpan offsetTime = TimeSpan.FromMilliseconds((double)offset);

                // Only do the work if we actually moved
                if (offsetTime != _globalTime)
                {
                    // Set the new global time
                    _globalTime = offsetTime;

                    // Adjust the starting time for future ticks
                    _startTime =_systemClock.CurrentTime - _globalTime;

View on GitHub (pinned to 81131a70a4)