Page 1 of 1

How to gracefully close all dialogs on Application exit

Posted: Fri Sep 14, 2018 7:37 pm
by deepti
Hi All,

I have a wxDialog instance which on click of a button, creates another instance of wxDialog, and this in turn creates another wxDialog on click of a button. It is basically a nested parent-child relationship.
Each wxDialog instance is a member variable of its parent class.
On application exit, should calling the Destroy() on the top-most parent wxDialog instance suffice? Will this take care of recursively calling Destroy() on all its child and grand-child dialogs?

Please let me know.

Re: How to gracefully close all dialogs on Application exit

Posted: Fri Sep 14, 2018 9:18 pm
by ONEEYEMAN
Hi,
How do you create those dialogs? Can you show some code?

Thank you.

Re: How to gracefully close all dialogs on Application exit

Posted: Mon Sep 17, 2018 5:37 am
by deepti
Hi,
Please find below the code explaining the creation of the dialogs.

So, there is a dialog "WxMainDialog" which has a hyperlink. On click of that, another dialog "WxAdvancedSettingsDialog" will be launched, as shown in the code below:
"m_asd" is member variable of class WxMainDialog.

Code: Select all

void WxMainDialog::OnSelectiveLink(wxHyperlinkEvent& WXUNUSED(event))
{
	m_asd = new WxAdvancedSettingsDialog(2, NULL);
	m_asd->SetFocus();
	m_asd->Raise();
	if (m_asd->ShowModal() == wxID_OK)
	{
	}
	m_asd->Destroy();
	m_asd = 0;
}

The "WxAdvancedSettingsDialog" dialog has an "Edit Schedule" button. On click of that, there is another "WxScheduleDialog" dialog which will be launched, as seen below:
"m_scheduleDlg" is member variable of class "WxAdvancedSettingsDialog".

Code: Select all

void WxAdvancedSettingsDialog::OnEditActiveSyncSchedule(wxCommandEvent& event)
{
	m_scheduleDlg = new WxScheduleDialog(TIME_ACTIVE_CONTROL);

	
	m_scheduleDlg->SetFocus();
	m_scheduleDlg->Raise();
	m_scheduleDlg->Show(true);

	.....
	.....
	if (m_scheduleDlg->ShowModal() == wxID_OK)
	{
		//do something	
	}
	m_scheduleDlg->Destroy();
}
The problem is when the last dialog (WxScheduleDialog), or even the second one "WxAdvancedSettingsDialog" is kept open, and the application is exited, the app does not exit properly. It is still lingering as can be seen in the Task Manager.
So, I need a way to destroy all these dialogs from the "OnExit" function of the app, where the uppermost dialog "WxMainDialog" is destroyed.
In the destructor of "WxMainDialog", I am calling the Destroy() on "WxAdvancedSettingsDialog" instance.
And in the destructor of "WxAdvancedSettingsDialog", i am calling the Destroy() on "WxScheduleDialog" instance.
Would this suffice?

Re: How to gracefully close all dialogs on Application exit

Posted: Mon Sep 17, 2018 2:47 pm
by ONEEYEMAN
Hi,
Which window is set to be the parent for the wxAdvancedeSettings Dialog and wxScheduleDialog?
Do they have a NULL parent or some window in your program hierarchy?

Thank you.

Re: How to gracefully close all dialogs on Application exit

Posted: Mon Sep 17, 2018 3:07 pm
by deepti
Yeah, they have NULL set as parent. Should that be changed to the dialog which is creating them?
Thank you!

Re: How to gracefully close all dialogs on Application exit

Posted: Mon Sep 17, 2018 3:12 pm
by doublemax
You could try wxTheApp->ExitMainLoop(). But i don't know if it works with open modal dialogs.

Re: How to gracefully close all dialogs on Application exit

Posted: Mon Sep 17, 2018 4:50 pm
by deepti
Hi,

The wxTheApp->ExitMainLoop() call didn't help :(
Should i set the parent of the dialog to the one that created it?
With this, it seems to be solving the issue, but it looks kinda weird, because the main dialog of the app gets closed before the child dialogs. But the app is exiting. But, is this a clean approach?

Please let me know..

Re: How to gracefully close all dialogs on Application exit

Posted: Mon Sep 17, 2018 6:19 pm
by ONEEYEMAN
Yes, it is a good approach.

Thank you.

Re: How to gracefully close all dialogs on Application exit

Posted: Tue Sep 18, 2018 8:04 pm
by art-ganseforth
Maybe you use sonething like this:

Code: Select all

void myDialog::OnCloseParent (wxEvent &ev) {
    ev.Skip();
    Close(); // maybe use Destoy() here
}

myDialog::myDialog (myDialog parent, ...) : wxDialog (...)  {
    parent->Connect(wxEVT_CLOSE, wxEvtHandler(myDialog::OnCloseParent ), NULL, this);

    ...

I wrote this out of my head. So something might be wrong, but i think the concept may work...

Best,
Frank