{"record":{"id":"0ead4742d3e1a689","repo":"dotnet/wpf","slug":"sr-cantsetownerwhoshwndisnotcreated","errorCode":null,"errorMessage":"SR.CantSetOwnerWhosHwndIsNotCreated","messagePattern":"SR\\.CantSetOwnerWhosHwndIsNotCreated","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":1243,"sourceCode":"                VerifyContextAndObjectState();\n                if (value == this)\n                {\n                    throw new ArgumentException(SR.CannotSetOwnerToItself);\n                }\n\n                if (_showingAsDialog)\n                {\n                    throw new InvalidOperationException(SR.CantSetOwnerAfterDialogIsShown);\n                }\n\n                if (value != null && value.IsSourceWindowNull)\n                {\n                    // Try to be specific in the error message.\n                    if (value._disposed)\n                    {\n                        throw new InvalidOperationException(SR.CantSetOwnerToClosedWindow);\n                    }\n                    throw new InvalidOperationException(SR.CantSetOwnerWhosHwndIsNotCreated);\n                }\n\n                if ( _ownerWindow == value )\n                {\n                    return;\n                }\n\n                if (!_disposed)\n                {\n                    // Check to see if value is already a child of this window.\n                    // If yes, throw Exception\n                    if (value != null)\n                    {\n                        WindowCollection ownedWindows = OwnedWindows;\n                        for (int i = 0; i < ownedWindows.Count; i++)\n                        {\n                            if (ownedWindows[i] == value)\n                            {","sourceCodeStart":1225,"sourceCodeEnd":1261,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L1225-L1261","documentation":"Window.Owner setter throws InvalidOperationException when the candidate owner window is non-null, not disposed, but its HWND has not been created yet (IsSourceWindowNull). A window whose Win32 handle does not exist cannot be an owner; WPF requires a real source window. This happens when the owner window was constructed but never shown.","triggerScenarios":"Assigning window.Owner to a window that was created but Show()/ShowDialog() has never been called, so its HWND is not yet created.","commonSituations":"Creating a parent window and a child dialog together and setting Owner before showing the parent; background threads constructing windows ahead of time.","solutions":["Call Show() (or ShowDialog()) on the owner window before assigning it as the Owner of another window.","Subscribe to the owner's SourceInitialized event and set Owner after that.","If the owner should never be visible, consider a hidden helper window that is explicitly shown with ShowInTaskbar=false and opacity 0 first."],"exampleFix":"// before\nvar owner = new MainWindow();\ndlg.Owner = owner; // HWND not created yet\n// after\nvar owner = new MainWindow();\nowner.Show();\ndlg.Owner = owner;","handlingStrategy":"validation","validationCode":"bool ownerReady(Window w) => w != null && w.IsLoaded; // IsLoaded implies source/HWND initialized after Show","typeGuard":null,"tryCatchPattern":"try { dlg.Owner = owner; }\ncatch (InvalidOperationException) { owner.SourceInitialized += (s,e) => dlg.Owner = owner; }","preventionTips":["Show the owner window before creating dependent dialogs.","Hook SourceInitialized to defer ownership assignment.","Do not pre-construct owner windows on background threads."],"tags":["wpf","window","owner","hwnd"],"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"}