dotnet/wpf · error · InvalidOperationException
SR.CantSetOwnerWhosHwndIsNotCreated
Error message
SR.CantSetOwnerWhosHwndIsNotCreated
What it means
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.
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.
Example fix
// before var owner = new MainWindow(); dlg.Owner = owner; // HWND not created yet // after var owner = new MainWindow(); owner.Show(); dlg.Owner = owner;
Defensive patterns
Strategy: validation
Validate before calling
bool ownerReady(Window w) => w != null && w.IsLoaded; // IsLoaded implies source/HWND initialized after Show
Try / catch
try { dlg.Owner = owner; }
catch (InvalidOperationException) { owner.SourceInitialized += (s,e) => dlg.Owner = owner; } Prevention
- Show the owner window before creating dependent dialogs.
- Hook SourceInitialized to defer ownership assignment.
- Do not pre-construct owner windows on background threads.
When it happens
Trigger: Assigning window.Owner to a window that was created but Show()/ShowDialog() has never been called, so its HWND is not yet created.
Common situations: Creating a parent window and a child dialog together and setting Owner before showing the parent; background threads constructing windows ahead of time.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.CannotSetOwnerToItself
- SR.CantSetOwnerAfterDialogIsShown
- SR.CantSetOwnerToClosedWindow
- SR.CircularOwnerChild
- SR.ChangeNotAllowedAfterShow
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0ead4742d3e1a689.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:1243
VerifyContextAndObjectState();
if (value == this)
{
throw new ArgumentException(SR.CannotSetOwnerToItself);
}
if (_showingAsDialog)
{
throw new InvalidOperationException(SR.CantSetOwnerAfterDialogIsShown);
}
if (value != null && value.IsSourceWindowNull)
{
// Try to be specific in the error message.
if (value._disposed)
{
throw new InvalidOperationException(SR.CantSetOwnerToClosedWindow);
}
throw new InvalidOperationException(SR.CantSetOwnerWhosHwndIsNotCreated);
}
if ( _ownerWindow == value )
{
return;
}
if (!_disposed)
{
// Check to see if value is already a child of this window.
// If yes, throw Exception
if (value != null)
{
WindowCollection ownedWindows = OwnedWindows;
for (int i = 0; i < ownedWindows.Count; i++)
{
if (ownedWindows[i] == value)
{View on GitHub (pinned to 81131a70a4)