{"record":{"id":"d4a40661be64e85f","repo":"dotnet/wpf","slug":"throw-new-invalidoperationexception-sr-hwndsubclass","errorCode":null,"errorMessage":"throw new InvalidOperationException(SR.HwndSubclassMultipleAttach);","messagePattern":"throw new InvalidOperationException\\(SR\\.HwndSubclassMultipleAttach\\);","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndSubclass.cs","lineNumber":120,"sourceCode":"        }\n\n        /// <summary>\n        ///     This method subclasses the specified window, such that the\n        ///     delegate specified to the constructor will be called to process\n        ///     the messages that are sent or posted to this window.\n        /// </summary>\n        /// <param name=\"hwnd\">\n        ///     The window to subclass.\n        /// </param>\n        /// <returns>\n        ///     An identifier that can be used to reference this instance of\n        ///     the HwndSubclass class in the static RequestDetach method.\n        /// </returns>\n        internal IntPtr Attach(IntPtr hwnd)\n        {\n\n            if (_bond != Bond.Unattached)\n                throw new InvalidOperationException(SR.HwndSubclassMultipleAttach);\n\n            return CriticalAttach( hwnd ) ;\n        }\n\n\n        /// <summary>\n        ///     This method unsubclasses this HwndSubclass object from the window\n        ///     it previously subclassed. The HwndSubclass object is not thread\n        ///     safe, and should thus be called only by the thread that owns\n        ///     the window being unsubclassed.\n        /// </summary>\n        /// <param name=\"force\">\n        ///     Whether or not the unsubclassing should be forced.  Due to the\n        ///     way that Win32 implements window subclassing, it is not always\n        ///     possible to safely remove a window proc from the WNDPROC chain.\n        ///     However, the delegate will not be called again after this\n        ///     method returns.\n        /// </param>","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndSubclass.cs#L102-L138","documentation":"HwndSubclass.Attach can only be called once per HwndSubclass instance. If the instance is already attached to a window (or detach is pending), calling Attach again throws InvalidOperationException (HwndSubclassMultipleAttach) because one instance may only bond to one HWND.","triggerScenarios":"Calling Attach(hwnd) twice on the same HwndWrapper/subclass instance without the window having been destroyed/detached; reattaching after a failed detach; sharing one wrapper across two HWNDs.","commonSituations":"Re-creating a window handle (Handle recursed) and reusing the old wrapper object; double-initialization of a control; window recreation during theme change reattaching a cached subclass.","solutions":["Create a new HwndWrapper/subclass instance for each new HWND instead of reattaching","Check/track attachment state before calling Attach; only attach when the instance is unattached","Ensure the previous detach (RequestDetach/Detach) completed before reusing the instance"],"exampleFix":"// before\nif (_hwndSubclass == null) { /* reuse */ } _hwndSubclass.Attach(hwnd); // second attach throws\n// after\n_hwndSubclass = new HwndWrapper(...); // fresh instance per HWND\n_hwndSubclass.Attach(hwnd);","handlingStrategy":"try-catch","validationCode":"if (hwndSubclass.IsAttached) return; // track attach state yourself\nhwndSubclass.Attach(hwnd);","typeGuard":null,"tryCatchPattern":"try { hwndSubclass.Attach(hwnd); } catch (InvalidOperationException) { /* already attached: allocate a fresh instance */ }","preventionTips":["One HwndWrapper/subclass instance per HWND","Never cache and reuse subclass objects across window recreation","Track attach state before calling Attach"],"tags":["csharp","wpf","win32","invalid-state"],"backgroundTag":"invalid-state-transition","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"}