dotnet/wpf · error · InvalidEnumArgumentException

The value of argument 'max' (max) is invalid for Enum type…

Error message

The value of argument 'max' (max) is invalid for Enum type 'DispatcherPriority'.

What it means

PriorityRange.Initialize validates that the 'max' bound is a defined DispatcherPriority between DispatcherPriority.Invalid (-1) and DispatcherPriority.Send (10). Out-of-range values raise InvalidEnumArgumentException naming 'max', its integer value, and typeof(DispatcherPriority).

Solutions

  1. Ensure the integer cast to DispatcherPriority for max is within -1..10.
  2. Use named DispatcherPriority constants for max (commonly DispatcherPriority.Send as the upper bound).
  3. Validate persisted/config values before constructing the range; reject or clamp out-of-range ints with logging.
  4. Remember Inactive as max is separately rejected with ArgumentException (error 5313).

Example fix

// before
var range = new PriorityRange(DispatcherPriority.Normal, true, (DispatcherPriority)maxRaw, true);
// after
if (maxRaw >= (int)DispatcherPriority.Invalid && maxRaw <= (int)DispatcherPriority.Send)
{
    var range = new PriorityRange(DispatcherPriority.Normal, true, (DispatcherPriority)maxRaw, true);
}
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidMaxPriority(DispatcherPriority p) =>
    p >= DispatcherPriority.Invalid && p <= DispatcherPriority.Send && p != DispatcherPriority.Inactive;

if (!IsValidMaxPriority(max)) throw new ArgumentOutOfRangeException(nameof(max), (int)max, "max must be a valid DispatcherPriority (-1..10, excluding Inactive).");

Type guard

static bool IsWithinPriorityRange(int value) =>
    value >= (int)DispatcherPriority.Invalid && value <= (int)DispatcherPriority.Send;

Try / catch

try
{
    var range = new PriorityRange(min, true, max, true);
}
catch (System.ComponentModel.InvalidEnumArgumentException ex)
{
    logger.LogError(ex, "PriorityRange max is not a valid DispatcherPriority: {Raw}", rawMax);
    throw; // config error — fail fast
}

Prevention

When it happens

Trigger: Calling a PriorityRange constructor with a max value cast from an arbitrary int (e.g. (DispatcherPriority)11 or higher), or from a value produced by arithmetic on enum constants.

Common situations: Persisted priority integers from older or newer framework versions; computed priorities like (DispatcherPriority)(normal + 1) that overflow past Send; deserialization of raw ints from XML/JSON config.

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/d2c03504b08fcdf9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/PriorityRange.cs:277

            {
                throw new ArgumentException(SR.InvalidPriority, nameof(min));
            }

            /*            
            if(max == null)
            {
                throw new ArgumentNullException("max");
            }

            if (!max.IsValid)
            {
                throw new ArgumentException("Invalid priority.", "max");
            }
            */
            if(max < DispatcherPriority.Invalid || max > DispatcherPriority.Send)
            {
                // If we move to a Priority class, this exception will have to change too.
                throw new System.ComponentModel.InvalidEnumArgumentException("max", (int)max, typeof(DispatcherPriority));
            }
            if(max == DispatcherPriority.Inactive)
            {
                throw new ArgumentException(SR.InvalidPriority, nameof(max));
            }
            
            if (max < min)
            {
                throw new ArgumentException(SR.InvalidPriorityRangeOrder);
            }

            _min = min;
            _isMinInclusive = isMinInclusive;
            _max = max;
            _isMaxInclusive = isMaxInclusive;
        }

        // This is a constructor for our special static members.

View on GitHub (pinned to 81131a70a4)