{"record":{"id":"2199c26a9cf4b4cf","repo":"dotnet/wpf","slug":"timeout","errorCode":null,"errorMessage":"timeout","messagePattern":"timeout","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs","lineNumber":571,"sourceCode":"        ///     A cancellation token that can be used to cancel the operation.\n        ///     If the operation has not started, it will be aborted when the\n        ///     cancellation token is canceled.  If the operation has started,\n        ///     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        public void Invoke(Action 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":553,"sourceCodeEnd":589,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs#L553-L589","documentation":"Dispatcher.Invoke validates the TimeSpan timeout argument and throws ArgumentOutOfRangeException (message shown as the parameter name 'timeout') when the timeout is negative and not exactly TimeSpan.FromMilliseconds(-1). -1 is the sentinel meaning 'infinite timeout'; any other negative value is rejected.","triggerScenarios":"Calling Dispatcher.Invoke(cb, priority, timeout, ...) with a negative TimeSpan such as TimeSpan.FromSeconds(-1) or TimeSpan.FromMilliseconds(-500); passing a computed TimeSpan that came out negative.","commonSituations":"Computing a remaining timeout from a deadline that already elapsed; typos like -TimeSpan.FromSeconds(5); migrating code that used 0/-1 int milliseconds into TimeSpan overloads with a stray negative.","solutions":["Pass TimeSpan.FromMilliseconds(-1) for an infinite timeout, or a non-negative TimeSpan.","Clamp the timeout: if (ts < TimeSpan.Zero) ts = TimeSpan.FromMilliseconds(-1);","Fix the calculation producing the negative TimeSpan (elapsed deadlines, subtraction order).","Use the Invoke overloads without a timeout parameter when a default is acceptable."],"exampleFix":"// before\ndispatcher.Invoke(action, DispatcherPriority.Normal, TimeSpan.FromSeconds(-1));\n// after\ndispatcher.Invoke(action, DispatcherPriority.Normal, TimeSpan.FromMilliseconds(-1)); // infinite","handlingStrategy":"validation","validationCode":"if (timeout < TimeSpan.Zero && timeout != TimeSpan.FromMilliseconds(-1))\n    timeout = TimeSpan.FromMilliseconds(-1); // or clamp to Zero\ndispatcher.Invoke(work, priority, timeout);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use TimeSpan.FromMilliseconds(-1) as the only negative timeout (infinite sentinel)","Clamp or normalize computed TimeSpans before passing to Invoke","Verify DateTime arithmetic used to derive remaining timeouts cannot go negative","Prefer timeout-less Invoke overloads when the default is acceptable"],"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"}