Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a WPF application which I have 3 windows.

The MainWindow, a Settings window and another window used as dialogWindow (custom modeless)

The Settings window opens only from mainwindow, if 2 minutes of inactivity is detected on Settings window this automatically close. [Upto here is ok], but if lets say this Settings Window trigerred a dialogWindow, if 2 minutes of inactivity is detected, only the Settings Window is being closed while the dialog is still there.

How can I close the dialog window as well?

This is how I am displaying the custom dialog on settings window

C#
var customDialog = new CustomDialog("Incorrect Password!", CustomDialog.DialogButton.OK, CustomDialog.DialogImage.Error);
                    customDialogResult = customDialog.ShowCustomDialog();
                    if (customDialogResult == true)
                    {
                        customDialog.Close();                       
                        this.IsEnabled = true;
                    }


And practically when inactivity is detected on settings page it just does this.close

What I have tried:

tried to implement an event but without sucess
Posted
Updated 20-May-16 9:33am
Comments
George Swan 22-May-16 15:53pm    
I suggest you have a look at how to use interaction requests to implement popup Windows using the Prism framework. It avoids the sort of problems you are getting as the dialog is implemented as a user control that's a child of the parent window.
https://msdn.microsoft.com/en-us/library/ff921081(v=pandp.40).aspx

You can use the Application.Windows collection
There is sample code for using the collection in the documentation
Application.Windows Property (System.Windows)[^]

You could put the code into the Closing event for your window
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-May-16 15:34pm    
Yes, but why? Everything is closed on closing of the main window anyway. Please see Solution 2.
—SA
CHill60 20-May-16 20:35pm    
OP does not appear to be closing the app. Just a form that has opened another. They are clearly observing a behaviour that they need a solution for. ..I believe my solution gives them a way to address that problem
Sergey Alexandrovich Kryukov 20-May-16 20:42pm    
Not sure, just because the question title says "...when parent window is closed". Anyway, your solution may help if this is not the case in fact. I voted 4 this time.
—SA
For windows, there is no such relationship as child-parent.

There is main window and other windows, this is all which makes the difference. Also, there is an optional ownership relationship, but it's irrelevant to the issue. What window instance is main is defined by current Application object, this is the window which was passed as a parameter to Application.Run. All other windows can be shown via Window.Show or Window.ShowDialog. The second case simulates modal behavior. So, even the same Window instance can be shown in a modal or not modal way. I have no idea of your CustomDialog type; you should have shown it. The recommended way of doing custom dialogs are using the same very Window class as a base class.

That's all. Now, the default behavior is this: if you close a non-main window shown via Window.Show, you just close it and effectively lose it. You can prevent is and hide it instead, to show later; this is one of the most popular technique. If the window is shown via Window.ShowDialog, you just close it, and, when it is modal, you cannot touch any other window, not main, not any one else.

And then, in contrast to all the above, by default, closing the main window not only closes all the window, it also exits the application — unless you prevent it. If you prevented it and don't realize how, nothing could help except getting to understand each like you write. You did not show anything like that, anyway. In other words, by default, there is nothing you need to do. If you exit the application, it exists; there are no windows, nothing.

Yes, you can prevent it. Then you can exist the application explicitly. In this case, there is no a need to close other windows explicitly using Application.Shutdown. See also:
Application.ShutdownMode Property (System.Windows)[^].

It's possible that the attempt to close another window prevents application shutdown. If you made some effort to prevent closing of a window, the exclusion should be made for all actions except closing by the user. This is how you could cancel closing. Normally, it's done only on some conditions (data wasn't saved, and loosing data is not confirmed be the user):
Window.OnClosing Method (CancelEventArgs) (System.Windows)[^],
CancelEventArgs Class (System.ComponentModel)[^].

If you don't do anything like that, than again, there is nothing you need to do.

—SA
 
Share this answer
 
Comments
datt265 20-May-16 19:11pm    
what i am closing with a timeout is settings window not mainwindow. All windows are displayed with .Show() method, and i am not using any windows dialogs but a window as a dialog i.e modeless, and have done some logic to return response when clicked.
Sergey Alexandrovich Kryukov 20-May-16 20:44pm    
I forgot to mention that closing something after a period of inactivity would be apparent user experience abuse. I bet most users will found it way too intrusive. In all cases, if you really close main window, it all happens as I described.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900