{"record":{"id":"46c9f01de72afd02","repo":"dotnet/wpf","slug":"argumentoutofrangeexception-timeout-was-duration-automatic","errorCode":null,"errorMessage":"ArgumentOutOfRangeException (timeout was Duration.Automatic)","messagePattern":"ArgumentOutOfRangeException \\(timeout was Duration\\.Automatic\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs","lineNumber":230,"sourceCode":"\n        /// <summary>\n        ///     This method tries to lock the WriteableBitmap for the specified\n        ///     timeout and increments the lock count if successful.\n        /// </summary>\n        /// <param name=\"timeout\">\n        ///     The amount of time to wait while trying to acquire the lock.\n        ///     To block indefinitely pass Duration.Forever.\n        ///     Duration.Automatic is an invalid value.\n        /// </param>\n        /// <returns>Returns true if the lock is now held, false otherwise.</returns>\n        public bool TryLock(Duration timeout)\n        {\n            WritePreamble();\n\n            TimeSpan timeoutSpan;\n            if (timeout == Duration.Automatic)\n            {\n                throw new ArgumentOutOfRangeException(nameof(timeout));\n            }\n            else if (timeout == Duration.Forever)\n            {\n                timeoutSpan = TimeSpan.FromMilliseconds(-1);\n            }\n            else\n            {\n                timeoutSpan = timeout.TimeSpan;\n            }\n\n            if (_lockCount == UInt32.MaxValue)\n            {\n                throw new InvalidOperationException(SR.Image_LockCountLimit);\n            }\n\n            if (_lockCount == 0)\n            {\n                // Try to acquire the back buffer by the supplied timeout, if the acquire call times out, return false.","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs#L212-L248","documentation":"WriteableBitmap.TryLock validates the lock timeout before attempting to acquire the back buffer. Duration.Automatic is not a real duration (it means 'defer to animation'), so passing it cannot be honored as a wait time and the library throws ArgumentOutOfRangeException. Callers must pass Duration.Forever, a finite Duration, or omit it.","triggerScenarios":"Calling TryLock(Duration.Automatic) or TryLock(rect, Duration.Automatic) — typically because a Duration was built via Duration.Automatic or taken from an animation property instead of a concrete timespan.","commonSituations":"Developers copy a Duration from an animated/timeline property (which is often Automatic) and pass it to TryLock; also occurs when a shared helper builds Durations with default Automatic semantics.","solutions":["Pass Duration.Forever to wait indefinitely: bitmap.TryLock(Duration.Forever).","Pass a concrete finite Duration such as new Duration(TimeSpan.FromMilliseconds(100)).","Check the Duration with duration.HasTimeSpan / == Duration.Automatic before passing and substitute a real duration.","Switch to the parameterless Lock() if you do not need timeout semantics."],"exampleFix":"// before\nif (!bitmap.TryLock(Duration.Automatic)) { ... }\n// after\nif (!bitmap.TryLock(Duration.Forever)) { ... }","handlingStrategy":"validation","validationCode":"if (timeout == Duration.Automatic) timeout = Duration.Forever; // or a concrete TimeSpan duration\nif (!timeout.HasTimeSpan && timeout != Duration.Forever) throw new ArgumentException(\"timeout must be Forever or a finite duration\");","typeGuard":"bool IsValidLockTimeout(Duration d) => d == Duration.Forever || d.HasTimeSpan;","tryCatchPattern":null,"preventionTips":["Never pass Durations sourced from animations directly to locking APIs","Default to Duration.Forever unless a real timeout is needed","Centralize lock acquisition in one helper that sanitizes the timeout"],"tags":["wpf","bitmap","argument-exception"],"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"}