{"record":{"id":"4013ee1dfe003e8d","repo":"dotnet/wpf","slug":"invalid-priority-value-dispatchertimer","errorCode":null,"errorMessage":"Invalid priority value.","messagePattern":"Invalid priority value\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherTimer.cs","lineNumber":229,"sourceCode":"            get\n            {\n                return _tag;\n            }\n\n            set\n            {\n                _tag = value;\n            }\n        }\n\n\n        private void Initialize(Dispatcher dispatcher, DispatcherPriority priority, TimeSpan interval)\n        {\n            // Note: all callers of this have a \"priority\" parameter.\n            Dispatcher.ValidatePriority(priority, \"priority\");\n            if(priority == DispatcherPriority.Inactive)\n            {\n                throw new ArgumentException(SR.InvalidPriority, nameof(priority));\n            }\n\n            _dispatcher = dispatcher;\n            _priority = priority;\n            _interval = interval;\n        }\n        \n        private void Restart()\n        {\n            lock(_instanceLock)\n            {\n                if (_operation != null)\n                {\n                    // Timer has already been restarted, e.g. Start was called form the Tick handler.\n                    return;\n                }\n\n                // BeginInvoke a new operation.","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherTimer.cs#L211-L247","documentation":"DispatcherTimer.Initialize validates the priority twice: Dispatcher.ValidatePriority checks the value is a defined DispatcherPriority, and then Initialize rejects DispatcherPriority.Inactive specifically, because a timer with Inactive priority would never be processed. An invalid or Inactive priority throws ArgumentException/InvalidOperationException from the constructor.","triggerScenarios":"new DispatcherTimer(TimeSpan, (DispatcherPriority)99, callback, dispatcher) with an undefined priority value, or new DispatcherTimer(..., DispatcherPriority.Inactive, ...).","commonSituations":"Casting raw ints from config or interop into DispatcherPriority, refactoring code that stored priorities as numbers, or passing Inactive intending 'paused' semantics (which is IsEnabled's job).","solutions":["Pass a valid active priority such as DispatcherPriority.Normal or Background - never Inactive","Validate any config/interop integer with Enum.IsDefined(typeof(DispatcherPriority), value) before casting","Use timer.IsEnabled = false to pause a timer instead of Inactive priority","Centralize priority parsing in one helper that rejects unknown values early"],"exampleFix":"// before\nvar prio = (DispatcherPriority)configPriority; // e.g. 99 or Inactive\nvar timer = new DispatcherTimer(interval, prio, OnTick, dispatcher);\n// after\nvar prio = Enum.IsDefined(typeof(DispatcherPriority), configPriority)\n    && (DispatcherPriority)configPriority != DispatcherPriority.Inactive\n        ? (DispatcherPriority)configPriority : DispatcherPriority.Normal;\nvar timer = new DispatcherTimer(interval, prio, OnTick, dispatcher);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(DispatcherPriority), priorityValue) ||\n    (DispatcherPriority)priorityValue == DispatcherPriority.Inactive)\n    throw new ArgumentException(nameof(priorityValue));","typeGuard":"bool IsValidTimerPriority(DispatcherPriority p) =>\n    Enum.IsDefined(p) && p != DispatcherPriority.Inactive;","tryCatchPattern":"try\n{\n    var timer = new DispatcherTimer(interval, priority, callback, dispatcher);\n}\ncatch (ArgumentException ex)\n{\n    logger.LogError(ex, \"Invalid timer priority {Priority}\", priority);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"priority\"))\n{\n    // Inactive priority passed; choose an active priority\n}","preventionTips":["Only pass named DispatcherPriority constants, never raw ints from config","Validate enum values with Enum.IsDefined before casting","Use IsEnabled = false for paused timers instead of Inactive","Keep a single helper for parsing priorities from external input"],"tags":["invalid-enum-value","priority","timer","wpf"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}