{"record":{"id":"9e37acb7e8694073","repo":"dotnet/wpf","slug":"the-period-is-invalid-period-must-be-non-negative-and-less","errorCode":null,"errorMessage":"The period '' is invalid. Period must be non-negative and less than or equal to Int32.MaxValue milliseconds.","messagePattern":"The period '' is invalid\\. Period must be non-negative and less than or equal to Int32\\.MaxValue milliseconds\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherTimer.cs","lineNumber":72,"sourceCode":"        /// <param name=\"interval\">\n        ///     The interval to tick the timer after.\n        /// </param>\n        /// <param name=\"priority\">\n        ///     The priority to process the timer at.\n        /// </param>\n        /// <param name=\"callback\">\n        ///     The callback to call when the timer ticks.\n        /// </param>\n        /// <param name=\"dispatcher\">\n        ///     The dispatcher to use to process the timer.\n        /// </param>\n        public DispatcherTimer(TimeSpan interval, DispatcherPriority priority, EventHandler callback, Dispatcher dispatcher) // NOTE: should be Priority\n        {\n            ArgumentNullException.ThrowIfNull(callback);\n            ArgumentNullException.ThrowIfNull(dispatcher);\n\n            if (interval.TotalMilliseconds < 0)\n                throw new ArgumentOutOfRangeException(nameof(interval), SR.TimeSpanPeriodOutOfRange_TooSmall);\n\n            if (interval.TotalMilliseconds > Int32.MaxValue)\n                throw new ArgumentOutOfRangeException(nameof(interval), SR.TimeSpanPeriodOutOfRange_TooLarge);\n\n            Initialize(dispatcher, priority, interval);\n            \n            Tick += callback;\n            Start();\n        }\n\n        /// <summary>\n        ///     Gets the dispatcher this timer is associated with.\n        /// </summary>\n        public Dispatcher Dispatcher\n        {\n            get\n            {\n                return _dispatcher;","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherTimer.cs#L54-L90","documentation":"The DispatcherTimer constructor validates the interval TimeSpan: it must represent between 0 and Int32.MaxValue milliseconds. A negative interval throws ArgumentOutOfRangeException with the TimeSpanPeriodOutOfRange_TooSmall message (displayed here with an empty formatted period). The constructor cannot proceed with an unrepresentable period.","triggerScenarios":"new DispatcherTimer(TimeSpan.FromMilliseconds(-1), priority, callback, dispatcher) or any interval whose TotalMilliseconds < 0.","commonSituations":"Passing a TimeSpan from parsed configuration where the sign was lost, using Timeout.InfiniteTimeSpan (-1 ms) which is valid for System.Threading.Timer but not DispatcherTimer, or computing an interval by subtraction that underflows.","solutions":["Pass a non-negative TimeSpan, e.g. TimeSpan.FromMilliseconds(0) or a positive duration","Guard/clamp the configured value before constructing: if (interval < TimeSpan.Zero) interval = TimeSpan.Zero","If an 'infinite' timer is intended, construct the timer without starting it (disable via IsEnabled) instead of using Timeout.InfiniteTimeSpan","Fix the configuration/source that produced the negative value"],"exampleFix":"// before\nvar timer = new DispatcherTimer(Timeout.InfiniteTimeSpan, DispatcherPriority.Normal, OnTick, Dispatcher.CurrentDispatcher);\n// after\nvar timer = new DispatcherTimer(TimeSpan.FromSeconds(30), DispatcherPriority.Normal, OnTick, Dispatcher.CurrentDispatcher);","handlingStrategy":"validation","validationCode":"if (interval < TimeSpan.Zero || interval.TotalMilliseconds > Int32.MaxValue)\n    throw new ArgumentOutOfRangeException(nameof(interval));","typeGuard":"bool IsValidTimerInterval(TimeSpan t) =>\n    t >= TimeSpan.Zero && t.TotalMilliseconds <= Int32.MaxValue;","tryCatchPattern":"try\n{\n    var timer = new DispatcherTimer(interval, priority, callback, dispatcher);\n}\ncatch (ArgumentOutOfRangeException ex)\n{\n    logger.LogError(ex, \"Invalid timer interval: {Interval}\", interval);\n}","preventionTips":["Clamp intervals to [0, Int32.MaxValue] ms before constructing","Never pass Timeout.InfiniteTimeSpan to DispatcherTimer","Validate configured durations at load time","Watch for TimeSpan subtraction underflow"],"tags":["argument-out-of-range","timer","wpf","dispatcher"],"backgroundTag":"argument-out-of-range","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"}