{"record":{"id":"10bb1982f8f0beb1","repo":"lepoco/wpfui","slug":"use-nameof-close-with-messageboxresult-instead","errorCode":null,"errorMessage":"Use {nameof(Close)} with MessageBoxResult instead","messagePattern":"Use (.+?) with MessageBoxResult instead","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/MessageBox/MessageBox.cs","lineNumber":297,"sourceCode":"\n    protected TaskCompletionSource<MessageBoxResult>? Tcs { get; set; }\n\n    [Obsolete($\"Use {nameof(ShowDialogAsync)} instead\")]\n    public new void Show()\n    {\n        throw new InvalidOperationException($\"Use {nameof(ShowDialogAsync)} instead\");\n    }\n\n    [Obsolete($\"Use {nameof(ShowDialogAsync)} instead\")]\n    public new bool? ShowDialog()\n    {\n        throw new InvalidOperationException($\"Use {nameof(ShowDialogAsync)} instead\");\n    }\n\n    [Obsolete($\"Use {nameof(Close)} with MessageBoxResult instead\")]\n    public new void Close()\n    {\n        throw new InvalidOperationException($\"Use {nameof(Close)} with MessageBoxResult instead\");\n    }\n\n    /// <summary>\n    /// Displays a message box\n    /// </summary>\n    /// <returns><see cref=\"MessageBoxResult\"/></returns>\n    /// <exception cref=\"TaskCanceledException\">Thrown if the operation is canceled.</exception>\n    public async Task<MessageBoxResult> ShowDialogAsync(\n        bool showAsDialog = true,\n        CancellationToken cancellationToken = default\n    )\n    {\n        Tcs = new TaskCompletionSource<MessageBoxResult>();\n        CancellationTokenRegistration tokenRegistration = cancellationToken.Register(\n            o => Tcs.TrySetCanceled((CancellationToken)o!),\n            cancellationToken\n        );\n","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/MessageBox/MessageBox.cs#L279-L315","documentation":"MessageBox.Close() (the no-arg Window method) is hidden and hard-throws because closing a WPF UI MessageBox requires passing a MessageBoxResult so the internal TaskCompletionSource resolves with the user's choice. Calling the parameterless Close would shut the window without setting a result, leaving awaiters of ShowDialogAsync hanging — so the library forbids it.","triggerScenarios":"Calling messageBox.Close() (no args) on a WPF.UI MessageBox; auto-close logic that calls Close() on any open Window; a 'close all windows' routine that walks the window list.","commonSituations":"A 'close everything on logout' helper; user code that closes the box from a non-button handler; migration from classic MessageBox where Close() was used to dismiss; CI/automation that dismisses windows generically.","solutions":["Call messageBox.Close(MessageBoxResult.Ok) (or the appropriate result) so the TCS resolves.","In a generic 'close all windows' routine, special-case MessageBox and pass a default result.","Prefer letting the box's own buttons close it; only close programmatically with an explicit result.","If you must force-close without a result, cancel the ShowDialogAsync CancellationToken instead."],"exampleFix":"// before\nvar box = new MessageBox { Content = \"hi\" };\nbox.Close(); // throws\n\n// after\nvar box = new MessageBox { Content = \"hi\" };\nbox.Close(MessageBoxResult.Ok);","handlingStrategy":"validation","validationCode":"// Always pass a result when closing programmatically:\nbox.Close(MessageBoxResult.Ok);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call the parameterless Close() on a WPF.UI MessageBox.","Special-case MessageBox in generic 'close all windows' routines.","Prefer letting the dialog's buttons close it; cancel via CancellationToken if needed."],"tags":["wpf","messagebox","api-misuse","obsolete","lifecycle"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}