{"record":{"id":"54e3aa5d8e9a4019","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-54e3aa","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'period')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'period'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Concurrency/EventLoopScheduler.cs","lineNumber":186,"sourceCode":"            return si;\n        }\n\n        /// <summary>\n        /// Schedules a periodic piece of work on the designated thread.\n        /// </summary>\n        /// <typeparam name=\"TState\">The type of the state passed to the scheduled action.</typeparam>\n        /// <param name=\"state\">Initial state passed to the action upon the first iteration.</param>\n        /// <param name=\"period\">Period for running the work periodically.</param>\n        /// <param name=\"action\">Action to be executed, potentially updating the state.</param>\n        /// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"action\"/> is <c>null</c>.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"period\"/> is less than <see cref=\"TimeSpan.Zero\"/>.</exception>\n        /// <exception cref=\"ObjectDisposedException\">The scheduler has been disposed and doesn't accept new work.</exception>\n        public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)\n        {\n            if (period < TimeSpan.Zero)\n            {\n                throw new ArgumentOutOfRangeException(nameof(period));\n            }\n\n            if (action == null)\n            {\n                throw new ArgumentNullException(nameof(action));\n            }\n\n            return new PeriodicallyScheduledWorkItem<TState>(this, state, period, action);\n        }\n\n        private sealed class PeriodicallyScheduledWorkItem<TState> : IDisposable\n        {\n            private readonly TimeSpan _period;\n            private readonly Func<TState, TState> _action;\n            private readonly EventLoopScheduler _scheduler;\n            private readonly AsyncLock _gate = new();\n\n            private TState _state;","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Concurrency/EventLoopScheduler.cs#L168-L204","documentation":"EventLoopScheduler.SchedulePeriodic<TState>(state, period, action) throws ArgumentOutOfRangeException(nameof(period)) when period is negative (less than TimeSpan.Zero). A negative periodic interval is meaningless, so the scheduler rejects it before creating the PeriodicallyScheduledWorkItem.","triggerScenarios":"Calling SchedulePeriodic with TimeSpan.FromMilliseconds(-1) or a negative TimeSpan computed from configuration (e.g. subtracting durations, parsing '-5' from settings).","commonSituations":"Config files or app settings where a period value was entered/negated incorrectly; date math producing a negative interval; Timeout semantics (-1 = infinite) mistakenly reused as a period.","solutions":["Clamp the period: if (period < TimeSpan.Zero) period = TimeSpan.Zero; before calling.","Validate configuration values at startup and fail with a clear message.","Use TimeSpan.Zero or a positive value; zero means run on every scheduler tick."],"exampleFix":"// before\nvar period = TimeSpan.FromMilliseconds(config.PeriodMs); // may be negative\nscheduler.SchedulePeriodic(state, period, Tick);\n\n// after\nvar period = TimeSpan.FromMilliseconds(Math.Max(0, config.PeriodMs));\nscheduler.SchedulePeriodic(state, period, Tick);","handlingStrategy":"validation","validationCode":"if (period < TimeSpan.Zero)\n    throw new ArgumentException(\"period must be >= TimeSpan.Zero\", nameof(period));\nscheduler.SchedulePeriodic(state, period, action);","typeGuard":"static bool IsValidPeriod(TimeSpan p) => p >= TimeSpan.Zero;","tryCatchPattern":"try { scheduler.SchedulePeriodic(state, period, action); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"period\")\n{\n    // correct config value\n}","preventionTips":["Validate period values when loading configuration","Do not reuse Timeout.Infinite-style -1 sentinels as periods","Clamp parsed durations with Math.Max(TimeSpan.Zero, value)"],"tags":["argument-out-of-range","scheduler","periodic"],"backgroundTag":"argument-out-of-range","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}