dotnet/wpf · error · InvalidEnumArgumentException

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

Error message

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

What it means

PriorityRange.Initialize validates that the 'min' bound is a defined DispatcherPriority between DispatcherPriority.Invalid (-1) and DispatcherPriority.Send (10). If 'min' falls outside that range, an InvalidEnumArgumentException names the offending argument, its integer value, and the enum type. The older ArgumentException('Invalid priority.') path is commented out in favor of this richer exception.

Solutions

  1. Check the integer being cast to DispatcherPriority and ensure it lies within the valid range (-1 through 10).
  2. Use only named DispatcherPriority constants (Send, Normal, Background, ContextIdle, ApplicationIdle, SystemIdle, Inactive, Invalid, etc.) instead of raw ints.
  3. If the value comes from config or persistence, validate/clamp it before constructing the PriorityRange and log the raw value for diagnosis.
  4. Note that DispatcherPriority.Inactive is separately rejected with ArgumentException (see error 5311); pick a dispatchable priority if you need active processing.

Example fix

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

Strategy: validation

Validate before calling

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

if (!IsValidMinPriority(min)) throw new ArgumentOutOfRangeException(nameof(min), (int)min, "min must be a dispatchable DispatcherPriority (-1..10, excluding Inactive).");

Type guard

static bool IsDefinedDispatcherPriority(int value) =>
    Enum.IsDefined(typeof(DispatcherPriority), value) &&
    value >= (int)DispatcherPriority.Invalid && value <= (int)DispatcherPriority.Send;

Try / catch

try
{
    var range = new PriorityRange(min, isMinInclusive, max, isMaxInclusive);
}
catch (System.ComponentModel.InvalidEnumArgumentException ex)
{
    logger.LogError(ex, "PriorityRange min is not a valid DispatcherPriority");
    // fall back to a safe default range
}

Prevention

When it happens

Trigger: Calling any PriorityRange constructor (which delegates to Initialize) with a min value cast from an arbitrary int (e.g. (DispatcherPriority)20 or (DispatcherPriority)-5), or passing a stale/renamed enum constant that no longer maps to a valid priority.

Common situations: Loading priorities from config files or persisted settings where the integer was saved from a different WPF version's enum; interop code computing priorities arithmetically; bindings that deserialize user-supplied ints directly into DispatcherPriority.

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

Appendix: source

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

        }
        
        private void Initialize(DispatcherPriority min, bool isMinInclusive, DispatcherPriority max, bool isMaxInclusive) // NOTE: should be Priority
        {
            /*
            if(min == null)
            {
                throw new ArgumentNullException("min");
            }
            
            if (!min.IsValid)
            {
                throw new ArgumentException("Invalid priority.", "min");
            }
            */
            if(min < DispatcherPriority.Invalid || min > DispatcherPriority.Send)
            {
                // If we move to a Priority class, this exception will have to change too.
                throw new System.ComponentModel.InvalidEnumArgumentException("min", (int)min, typeof(DispatcherPriority));
            }
            if(min == DispatcherPriority.Inactive)
            {
                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)

View on GitHub (pinned to 81131a70a4)