{"record":{"id":"f6998b388df74323","repo":"dotnet/wpf","slug":"throw-new-argumentoutofrangeexception-nameof-timeout","errorCode":null,"errorMessage":"throw new ArgumentOutOfRangeException(nameof(timeout));","messagePattern":"throw new ArgumentOutOfRangeException\\(nameof\\(timeout\\)\\);","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/D3DImage.cs","lineNumber":237,"sourceCode":"        /// </summary>\n        public void Lock()\n        {\n            WritePreamble();\n\n            LockImpl(Duration.Forever);\n        }\n\n        /// <summary>\n        ///     Trys to lock the D3DImage but gives up once duration expires. Returns true\n        ///     if the lock was obtained. See Lock for more details.\n        /// </summary>\n        public bool TryLock(Duration timeout)\n        {\n            WritePreamble();\n\n            if (timeout == Duration.Automatic)\n            {\n                throw new ArgumentOutOfRangeException(nameof(timeout));\n            }\n\n            return LockImpl(timeout);\n        }\n\n        /// <summary>\n        ///     Unlocks the D3DImage\n        ///\n        ///     Can only be called while locked.\n        ///\n        ///     If you have dirtied the image with AddDirtyRect, Unlocking will trigger us to\n        ///     copy the dirty regions from the back buffer to the front buffer. While this is\n        ///     taking place, Lock will block. To avoid locking indefinitely, use TryLock.\n        /// </summary>\n        public void Unlock()\n        {\n            WritePreamble();\n            ","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/D3DImage.cs#L219-L255","documentation":"D3DImage.TryLock(Duration timeout) attempts to acquire the render lock without blocking indefinitely, so a Duration.Automatic timeout is meaningless and rejected: it throws ArgumentOutOfRangeException for parameter 'timeout'. Only finite durations and Duration.Forever are acceptable.","triggerScenarios":"Calling TryLock(new Duration()) (default Duration == Automatic); passing Duration.Automatic explicitly (e.g. reusing a Duration taken from an animation); constructing a Duration from a TimeSpan that was never set or via a default struct.","commonSituations":"Copying Duration values from animation timelines (where Automatic is legal) into TryLock; default-initialized Duration fields; timing code that computes a Duration lazily and never assigns it.","solutions":["Pass a concrete timeout, e.g. TryLock(TimeSpan.FromMilliseconds(50)) or TryLock(Duration.Forever) for blocking behavior identical to Lock().","Before calling, check `duration != Duration.Automatic` and substitute a real duration.","If you need a 'best effort, no wait' lock, use a very small finite duration such as Duration.Zero or TimeSpan.Zero."],"exampleFix":"// before\nDuration d = new Duration(); // Automatic\nd3dImage.TryLock(d); // ArgumentOutOfRangeException\n\n// after\nvar d = TimeSpan.FromMilliseconds(50);\nif (d3dImage.TryLock(d)) { try { /* render */ } finally { d3dImage.Unlock(); } }","handlingStrategy":"validation","validationCode":"if (duration == Duration.Automatic)\n    duration = Duration.Forever; // or a concrete TimeSpan\nbool got = d3dImage.TryLock(duration);","typeGuard":null,"tryCatchPattern":"try { return d3dImage.TryLock(timeout); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"timeout\")\n{\n    return d3dImage.TryLock(TimeSpan.FromMilliseconds(50));\n}","preventionTips":["Never reuse Duration values taken from animations for timeouts.","Use explicit TimeSpan values (or Duration.Forever) for TryLock.","Avoid default(Duration) — it equals Automatic."],"tags":["wpf","d3dimage","argument-out-of-range","duration"],"backgroundTag":"invalid-duration-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}