{"record":{"id":"ecc912df6e6fa99a","repo":"dotnet/wpf","slug":"the-value-of-argument-parametername-priority-is-invalid-for","errorCode":null,"errorMessage":"The value of argument 'parameterName' (priority) is invalid for Enum type 'DispatcherPriority'.","messagePattern":"The value of argument 'parameterName' \\(priority\\) is invalid for Enum type 'DispatcherPriority'\\.","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs","lineNumber":1544,"sourceCode":"        /// <param name=\"parameterName\">\n        ///     The name if the argument to report in the ArgumentException\n        ///     that is raised if the priority is not suitable for use by\n        ///     the dispatcher.\n        /// </param>\n        public static void ValidatePriority(DispatcherPriority priority, string parameterName) // NOTE: should be Priority\n        {\n            // First make sure the Priority is valid.\n            // Priority.ValidatePriority(priority, paramName);\n\n            // Second, make sure the priority is in a range recognized by\n            // the dispatcher.\n            if(!_foregroundPriorityRange.Contains(priority) &&\n               !_backgroundPriorityRange.Contains(priority) &&\n               !_idlePriorityRange.Contains(priority) &&\n               DispatcherPriority.Inactive != priority)  // NOTE: should be Priority.Min\n            {\n                // If we move to a Priority class, this exception will have to change too.\n                throw new System.ComponentModel.InvalidEnumArgumentException(parameterName, (int)priority, typeof(DispatcherPriority));\n            }\n        }\n\n        /// <summary>\n        ///     Checks that the calling thread has access to this object.\n        /// </summary>\n        /// <remarks>\n        ///     Only the dispatcher thread may access DispatcherObjects.\n        ///     <p/>\n        ///     This method is public so that any thread can probe to\n        ///     see if it has access to the DispatcherObject.\n        /// </remarks>\n        /// <returns>\n        ///     True if the calling thread has access to this object.\n        /// </returns>\n        [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]\n        public DispatcherHooks Hooks\n        {","sourceCodeStart":1526,"sourceCodeEnd":1562,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs#L1526-L1562","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass only defined DispatcherPriority members (Invalid through Inactive).","Validate before calling: Enum.IsDefined(typeof(DispatcherPriority), value).","Catch InvalidEnumArgumentException at boundaries where priorities come from external input and map them to a safe default like DispatcherPriority.Normal.","Fix code that computes priorities by integer math instead of using the enum constants."],"exampleFix":"// before\nvar prio = (DispatcherPriority)configValue; // e.g. 42\ndispatcher.Invoke(work, prio);\n// after\nvar prio = Enum.IsDefined(typeof(DispatcherPriority), configValue)\n    ? (DispatcherPriority)configValue\n    : DispatcherPriority.Normal;\ndispatcher.Invoke(work, prio);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(DispatcherPriority), value))\n    value = DispatcherPriority.Normal;\ndispatcher.Invoke(work, value);","typeGuard":"bool IsValidDispatcherPriority(object v) =>\n    v is DispatcherPriority p &&\n    Enum.IsDefined(typeof(DispatcherPriority), p);","tryCatchPattern":"try\n{\n    dispatcher.Invoke(work, priority);\n}\ncatch (InvalidEnumArgumentException ex)\n{\n    dispatcher.Invoke(work, DispatcherPriority.Normal); // safe default\n}","preventionTips":["Only use named DispatcherPriority members; never cast raw ints","Validate externally sourced priorities with Enum.IsDefined before use","Avoid integer arithmetic on enum values","Map invalid priorities to DispatcherPriority.Normal at deserialization boundaries"],"tags":["wpf","dispatcher","priority","invalid-enum","argument"],"backgroundTag":"invalid-enum-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}