{"record":{"id":"e05ea20117b3bb79","repo":"dotnet/reactive","slug":"argumentoutofrangeexception","errorCode":null,"errorMessage":"ArgumentOutOfRangeException","messagePattern":"ArgumentOutOfRangeException","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Timer.cs","lineNumber":41,"sourceCode":"        }\n\n        public static IAsyncObservable<long> Timer(DateTimeOffset dueTime)\n        {\n            return Create<long>(observer => AsyncObserver.Timer(observer, dueTime));\n        }\n\n        public static IAsyncObservable<long> Timer(DateTimeOffset dueTime, IAsyncScheduler scheduler)\n        {\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return Create<long>(observer => AsyncObserver.Timer(observer, dueTime, scheduler));\n        }\n\n        public static IAsyncObservable<long> Timer(TimeSpan dueTime, TimeSpan period)\n        {\n            if (period < TimeSpan.Zero)\n                throw new ArgumentOutOfRangeException(nameof(period));\n\n            return Create<long>(observer => AsyncObserver.Timer(observer, dueTime, period));\n        }\n\n        public static IAsyncObservable<long> Timer(TimeSpan dueTime, TimeSpan period, IAsyncScheduler scheduler)\n        {\n            if (period < TimeSpan.Zero)\n                throw new ArgumentOutOfRangeException(nameof(period));\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return Create<long>(observer => AsyncObserver.Timer(observer, dueTime, period, scheduler));\n        }\n\n        public static IAsyncObservable<long> Timer(DateTimeOffset dueTime, TimeSpan period)\n        {\n            if (period < TimeSpan.Zero)\n                throw new ArgumentOutOfRangeException(nameof(period));","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Timer.cs#L23-L59","documentation":"The Timer(TimeSpan, TimeSpan) overload requires a non-negative period. System.Reactive.Async validates the period eagerly and throws ArgumentOutOfRangeException when period < TimeSpan.Zero, since a repeating timer cannot tick at a negative interval.","triggerScenarios":"Calling AsyncObservable.Timer(dueTime, TimeSpan.FromSeconds(-1)) — any TimeSpan period whose value is negative.","commonSituations":"Period computed by subtracting timestamps (later - earlier reversed) or parsed from a user-supplied configuration value that is negative; sign errors in constants.","solutions":["Ensure the period argument is >= TimeSpan.Zero before calling Timer.","If the period is computed, clamp it: Math.Max(TimeSpan.Zero, computedPeriod).","Validate/normalize any configuration value feeding the period."],"exampleFix":"// before\nvar timer = AsyncObservable.Timer(dueTime, end - start); // end < start => negative\n// after\nvar period = end > start ? end - start : TimeSpan.Zero;\nvar timer = AsyncObservable.Timer(dueTime, period);","handlingStrategy":"validation","validationCode":"if (period < TimeSpan.Zero)\n    throw new InvalidOperationException(\"Timer period must be non-negative\");\nvar timer = AsyncObservable.Timer(dueTime, period);","typeGuard":"bool IsValidPeriod(TimeSpan p) => p >= TimeSpan.Zero;","tryCatchPattern":"try\n{\n    var timer = AsyncObservable.Timer(dueTime, period);\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"period\")\n{\n    timer = AsyncObservable.Timer(dueTime, TimeSpan.Zero);\n}","preventionTips":["Clamp computed durations with TimeSpan.Max or explicit comparison.","Validate interval config values at load time.","Check subtraction order when deriving periods from timestamps."],"tags":["argument-out-of-range","timespan","reactive","timer"],"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"}