dotnet/wpf · error · ArgumentOutOfRangeException
Specified argument was out of the range of valid values…
Error message
Specified argument was out of the range of valid values. (Parameter 'timeout')
What it means
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.
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.
Example fix
// before var ts = end - DateTime.Now; // may be negative dispatcher.Invoke(work, DispatcherPriority.Send, ts); // after var ts = end - DateTime.Now; if (ts < TimeSpan.Zero) ts = TimeSpan.FromMilliseconds(-1); dispatcher.Invoke(work, DispatcherPriority.Send, ts);
Defensive patterns
Strategy: validation
Validate before calling
if (timeout < TimeSpan.Zero && timeout != TimeSpan.FromMilliseconds(-1))
throw new ArgumentException("timeout must be >= 0 or -1ms", nameof(timeout));
dispatcher.Invoke(work, priority, timeout); Prevention
- 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
When it happens
Trigger: Invoking any Dispatcher.Invoke overload that accepts a TimeSpan timeout with timeout.TotalMilliseconds < 0 and timeout != TimeSpan.FromMilliseconds(-1).
Common situations: 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.
Understand the failure class
- Timeouts: ETIMEDOUT, deadlines, and hung requests — what actually expires when a request times out.
Related errors
- timeout
- SR.AutomationTimeout
- The operation has timed out.
- Argument out of range (end not contained in view)
- Argument out of range (position does not map to a line)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/83c31851a607dddb.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:713
/// the operation can cooperate with the cancellation request.
/// </param>
/// <param name="timeout">
/// The minimum amount of time to wait for the operation to start.
/// Once the operation has started, it will complete before this method
/// returns.
/// </param>
/// <returns>
/// The return value from the delegate being invoked.
/// </returns>
public TResult Invoke<TResult>(Func<TResult> callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout)
{
ArgumentNullException.ThrowIfNull(callback);
ValidatePriority(priority, "priority");
if( timeout.TotalMilliseconds < 0 &&
timeout != TimeSpan.FromMilliseconds(-1))
{
throw new ArgumentOutOfRangeException(nameof(timeout));
}
// Fast-Path: if on the same thread, and invoking at Send priority,
// and the cancellation token is not already canceled, then just
// call the callback directly.
if(!cancellationToken.IsCancellationRequested && priority == DispatcherPriority.Send && CheckAccess())
{
SynchronizationContext oldSynchronizationContext = SynchronizationContext.Current;
try
{
DispatcherSynchronizationContext newSynchronizationContext;
if(BaseCompatibilityPreferences.GetReuseDispatcherSynchronizationContextInstance())
{
newSynchronizationContext = _defaultDispatcherSynchronizationContext;
}
else
{View on GitHub (pinned to 81131a70a4)