In a WPF application there is a small information form that is called from the main one. As planned, this informational form should have a standard cross for closing, and the form should also close itself when the focus is lost. I use for this Window_Deactivated.
private void Window_Deactivated(object sender, EventArgs e) { this.Close(); } If the focus is lost, everything works as it should, but when you close the form through a cross, an exception occurs:
System.InvalidOperationException: 'While closing a window, you cannot set Visibility to Visible or call Show, ShowDialog, Close or WindowInteropHelper.EnsureHandle.'
Is it possible to somehow catch in Deactivated that the form is already closed, so as not to throw a re-closure and not to cause an exception?
Window_Closingto ignore theWindow_Deactivatedevent. Or, ifWindow_Deactivatedoccurs beforeWindow_Closingmake a custom button in the handler of which set a flan to prohibit the operation of the code from theWindow_Deactivatedevent. - Rootware