{"record":{"id":"83c31851a607dddb","repo":"dotnet/wpf","slug":"specified-argument-was-out-of-the-range-of-valid-values-83c318","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'timeout')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'timeout'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs","lineNumber":713,"sourceCode":"        ///     the operation can cooperate with the cancellation request.\n        /// </param>\n        /// <param name=\"timeout\">\n        ///     The minimum amount of time to wait for the operation to start.\n        ///     Once the operation has started, it will complete before this method\n        ///     returns.\n        /// </param>\n        /// <returns>\n        ///     The return value from the delegate being invoked.\n        /// </returns>\n        public TResult Invoke<TResult>(Func<TResult> callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout)\n        {\n            ArgumentNullException.ThrowIfNull(callback);\n            ValidatePriority(priority, \"priority\");\n\n            if( timeout.TotalMilliseconds < 0 &&\n                timeout != TimeSpan.FromMilliseconds(-1))\n            {\n                throw new ArgumentOutOfRangeException(nameof(timeout));\n            }\n\n            // Fast-Path: if on the same thread, and invoking at Send priority,\n            // and the cancellation token is not already canceled, then just\n            // call the callback directly.\n            if(!cancellationToken.IsCancellationRequested && priority == DispatcherPriority.Send && CheckAccess())\n            {\n                SynchronizationContext oldSynchronizationContext = SynchronizationContext.Current;\n\n                try\n                {\n                    DispatcherSynchronizationContext newSynchronizationContext;\n                    if(BaseCompatibilityPreferences.GetReuseDispatcherSynchronizationContextInstance())\n                    {\n                        newSynchronizationContext = _defaultDispatcherSynchronizationContext;\n                    }\n                    else\n                    {","sourceCodeStart":695,"sourceCodeEnd":731,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs#L695-L731","documentation":"This is the same Dispatcher.Invoke timeout validation as error 5294, with the fully formatted ArgumentOutOfRangeException message: 'Specified argument was out of the range of valid values. (Parameter timeout)'. The dispatcher rejects negative timeouts other than the -1ms infinite sentinel because a negative wait is undefined for the internal event wait used while pumping.","triggerScenarios":"Invoking any Dispatcher.Invoke overload that accepts a TimeSpan timeout with timeout.TotalMilliseconds < 0 and timeout != TimeSpan.FromMilliseconds(-1).","commonSituations":"Passing a TimeSpan derived from DateTime arithmetic that went negative; deserializing timeouts from config where a negative sneaks in; converting legacy int milliseconds timeouts (-1) incorrectly into TimeSpan.MinValue or other negative values.","solutions":["Use TimeSpan.FromMilliseconds(-1) to express an infinite wait; use >= TimeSpan.Zero otherwise.","Validate/normalize the timeout before calling Invoke.","Correct the upstream code/config that produced the negative value.","Prefer timeout-less Invoke overloads when infinite waiting is intended anyway."],"exampleFix":"// before\nvar ts = end - DateTime.Now; // may be negative\ndispatcher.Invoke(work, DispatcherPriority.Send, ts);\n// after\nvar ts = end - DateTime.Now;\nif (ts < TimeSpan.Zero) ts = TimeSpan.FromMilliseconds(-1);\ndispatcher.Invoke(work, DispatcherPriority.Send, ts);","handlingStrategy":"validation","validationCode":"if (timeout < TimeSpan.Zero && timeout != TimeSpan.FromMilliseconds(-1))\n    throw new ArgumentException(\"timeout must be >= 0 or -1ms\", nameof(timeout));\ndispatcher.Invoke(work, priority, timeout);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate timeouts at your API boundary before calling Dispatcher.Invoke","Never map legacy -1 int timeouts to arbitrary negative TimeSpans; use FromMilliseconds(-1)","Load timeouts from config as non-negative values with explicit infinite sentinel handling","Add unit tests covering negative and zero timeout inputs"],"tags":["wpf","dispatcher","argumentoutofrange","timeout"],"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"}