{"record":{"id":"a4f9cdcb81ec2543","repo":"dotnet/wpf","slug":"sr-invalideventhandle","errorCode":null,"errorMessage":"SR.InvalidEventHandle","messagePattern":"SR\\.InvalidEventHandle","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/ByteRangeDownloader.cs","lineNumber":411,"sourceCode":"\n        /// <summary>\n        /// Constructor for ByteRangeDownloader\n        /// </summary>\n        private ByteRangeDownloader(Uri requestedUri, SafeWaitHandle eventHandle)\n        {\n            ArgumentNullException.ThrowIfNull(requestedUri);\n\n            // Ensure uri is correct scheme (http or https) Do case-sensitive comparison since Uri.Scheme contract is to return in lower case only.\n            if (!string.Equals(requestedUri.Scheme, Uri.UriSchemeHttp, StringComparison.Ordinal) && !string.Equals(requestedUri.Scheme, Uri.UriSchemeHttps, StringComparison.Ordinal))\n            {\n                throw new ArgumentException(SR.InvalidScheme, nameof(requestedUri));\n            }\n\n            ArgumentNullException.ThrowIfNull(eventHandle);\n\n            if (eventHandle.IsInvalid || eventHandle.IsClosed)\n            {\n                throw new ArgumentException(SR.InvalidEventHandle, nameof(eventHandle));\n            }\n\n            _requestedUri = requestedUri;\n            _eventHandle = eventHandle;\n        }\n\n        /// <summary>\n        /// Check if it has been errored out from the worker thread and re-throw the exception that was\n        /// thrown from the worker thread\n        /// </summary>\n        /// <remarks>No need to lock in this function since the caller always locks before making this call</remarks>\n        private void CheckErroredOutCondition()\n        {\n            if (_erroredOut)\n            {\n                throw new InvalidOperationException(SR.ByteRangeDownloaderErroredOut, _erroredOutException);\n            }\n        }","sourceCodeStart":393,"sourceCodeEnd":429,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/ByteRangeDownloader.cs#L393-L429","documentation":"ByteRangeDownloader's constructor requires a valid, open SafeFileHandle-backed event handle; it signals this handle when asynchronous download work completes. If the handle is null, invalid, or already closed, it throws ArgumentException keyed by SR.InvalidEventHandle.","triggerScenarios":"Calling the public ByteRangeDownloader constructor with an eventHandle that is null, has IsInvalid==true, or IsClosed==true (e.g. a handle disposed elsewhere or never created via CreateEvent).","commonSituations":"The event handle was disposed by another thread before the constructor ran, or a failed CreateEvent (returns null/invalid) result is passed through unchecked.","solutions":["Create a fresh valid AutoResetEvent/ManualResetEvent handle (via its SafeWaitHandle) and pass it before any disposal","Check eventHandle.IsInvalid/IsClosed before constructing","If the handle was closed, recreate it rather than reusing the stale handle"],"exampleFix":"// before\nvar ev = new AutoResetEvent(false);\nev.Dispose();\nnew ByteRangeDownloader(uri, ev.SafeWaitHandle, timeout);\n// after\nusing var ev = new AutoResetEvent(false);\nif (!ev.SafeWaitHandle.IsInvalid && !ev.SafeWaitHandle.IsClosed)\n    new ByteRangeDownloader(uri, ev.SafeWaitHandle, timeout);","handlingStrategy":"validation","validationCode":"bool validHandle = h != null && !h.IsInvalid && !h.IsClosed;","typeGuard":"static bool IsValidWaitHandle(SafeWaitHandle? h) => h is { IsInvalid: false, IsClosed: false };","tryCatchPattern":"try { var d = new ByteRangeDownloader(uri, handle, timeout); } catch (ArgumentException ex) when (ex.ParamName == \"eventHandle\") { handle = new AutoResetEvent(false).SafeWaitHandle; }","preventionTips":["Keep the event handle alive for the downloader's lifetime","Never pass disposed SafeWaitHandles; recreate instead"],"tags":["win32","handle","wpf","argumentexception"],"backgroundTag":"invalid-argument-value","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"}