dotnet/wpf · error · InvalidEnumArgumentException

The value of argument 'parameterName' (priority) is invalid…

Error message

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

What it means

Dispatcher.ValidatePriority throws System.ComponentModel.InvalidEnumArgumentException stating that the value of argument 'priority' is invalid for enum type DispatcherPriority, when the priority is not inside the foreground range (Invalid..Send), the background range (ContextIdle..Background), the idle range (SystemIdle/SystemInactive), nor equal to DispatcherPriority.Inactive — i.e. an out-of-range or undefined enum value (often an arbitrary int cast to DispatcherPriority).

Solutions

  1. Pass only defined DispatcherPriority members (Invalid through Inactive).
  2. Validate before calling: Enum.IsDefined(typeof(DispatcherPriority), value).
  3. Catch InvalidEnumArgumentException at boundaries where priorities come from external input and map them to a safe default like DispatcherPriority.Normal.
  4. Fix code that computes priorities by integer math instead of using the enum constants.

Example fix

// before
var prio = (DispatcherPriority)configValue; // e.g. 42
dispatcher.Invoke(work, prio);
// after
var prio = Enum.IsDefined(typeof(DispatcherPriority), configValue)
    ? (DispatcherPriority)configValue
    : DispatcherPriority.Normal;
dispatcher.Invoke(work, prio);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(DispatcherPriority), value))
    value = DispatcherPriority.Normal;
dispatcher.Invoke(work, value);

Type guard

bool IsValidDispatcherPriority(object v) =>
    v is DispatcherPriority p &&
    Enum.IsDefined(typeof(DispatcherPriority), p);

Try / catch

try
{
    dispatcher.Invoke(work, priority);
}
catch (InvalidEnumArgumentException ex)
{
    dispatcher.Invoke(work, DispatcherPriority.Normal); // safe default
}

Prevention

When it happens

Trigger: Calling Dispatcher.Invoke/InvokeAsync/BeginInvoke/Yield (or anything that calls ValidatePriority) with a DispatcherPriority value outside the defined enum members, typically via an unchecked cast like (DispatcherPriority)42 or a corrupted/defaulted int from serialization or P/Invoke.

Common situations: Priorities read from config/strings that were parsed with Enum.ToObject without validation; arithmetic on priorities; interop layers passing raw ints; JSON/binary deserialization reconstructing invalid enum values.

Understand the failure class

Background: "invalid argument", "unknown mode", "not supported": invalid enum-like argument errors explained — this error's family across 19 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:1544

        /// <param name="parameterName">
        ///     The name if the argument to report in the ArgumentException
        ///     that is raised if the priority is not suitable for use by
        ///     the dispatcher.
        /// </param>
        public static void ValidatePriority(DispatcherPriority priority, string parameterName) // NOTE: should be Priority
        {
            // First make sure the Priority is valid.
            // Priority.ValidatePriority(priority, paramName);

            // Second, make sure the priority is in a range recognized by
            // the dispatcher.
            if(!_foregroundPriorityRange.Contains(priority) &&
               !_backgroundPriorityRange.Contains(priority) &&
               !_idlePriorityRange.Contains(priority) &&
               DispatcherPriority.Inactive != priority)  // NOTE: should be Priority.Min
            {
                // If we move to a Priority class, this exception will have to change too.
                throw new System.ComponentModel.InvalidEnumArgumentException(parameterName, (int)priority, typeof(DispatcherPriority));
            }
        }

        /// <summary>
        ///     Checks that the calling thread has access to this object.
        /// </summary>
        /// <remarks>
        ///     Only the dispatcher thread may access DispatcherObjects.
        ///     <p/>
        ///     This method is public so that any thread can probe to
        ///     see if it has access to the DispatcherObject.
        /// </remarks>
        /// <returns>
        ///     True if the calling thread has access to this object.
        /// </returns>
        [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
        public DispatcherHooks Hooks
        {

View on GitHub (pinned to 81131a70a4)