Page 1 of 1

App exit seamlessly when system powered off

Posted: Tue Aug 06, 2019 12:38 pm
by deepti
Hi All,

We have a wxWidgets based application. When user clicks "Exit" on the app, it exits smoothly without any crash.
However, when the PC is shutdown while the app is running, the app crashes.
Is there any event sent when the PC is shutdown, which can be caught by our application and then exit smoothly?

Please help!

Re: App exit seamlessly when system powered off

Posted: Tue Aug 06, 2019 2:26 pm
by ONEEYEMAN
Hi,
Look here.

Thank you.

Re: App exit seamlessly when system powered off

Posted: Tue Aug 06, 2019 3:56 pm
by doublemax
A wxPowerEvent is only sent with regards to system suspending / hibernating.

For power down events you also need to check EVT_QUERY_END_SESSION/EVT_END_SESSION:
https://docs.wxwidgets.org/trunk/classw ... event.html

Re: App exit seamlessly when system powered off

Posted: Wed Aug 21, 2019 7:43 pm
by deepti
Thank you for your responses.
I am a bit unclear on which all events need to be handled. Basically looking for clean exit when system is powered off when app is running.
Could you elaborate with some sample code please?

Thank you!

Re: App exit seamlessly when system powered off

Posted: Wed Aug 21, 2019 8:45 pm
by doublemax
First of all, the reason that your program crashes is most likely because the order of object destruction can be different when the app if forcefully closed by the OS. Unfortunately this is very hard to debug as it only happens when the system is shutting down. For a test, can try calling wxApp::OnExit() and check if it also triggers a crash. (Sometimes it does).

As for the event handler, just catch wxEVT_END_SESSION event in the wxApp(!) object, not in a window.
Try the two options i show below.

Code: Select all

void MyApp::OnAppEndSession(wxCloseEvent &event)
{
   // option 1: close mainframe
   m_frame->Close();
   
   // option 2:
   wxTheApp->ExitMainLoop();
}

Re: App exit seamlessly when system powered off

Posted: Fri Aug 23, 2019 8:30 am
by deepti
Hi @doublemax,

Thanks a ton for your response.
Here is my code. WxSyncApp derives from wxApp.

Code: Select all

BEGIN_EVENT_TABLE(WxSyncApp, wxApp)
EVT_END_SESSION(WxSyncApp::OnAppEndSession)
END_EVENT_TABLE()

void WxSyncApp::OnAppEndSession(wxCloseEvent &event)
{
	// option 1: close mainframe
	//m_frame->Close();

	// option 2:
	wxTheApp->ExitMainLoop();
}
And i am using a tool called "Send Message" which can be used to send Windows messages to an application.
With this tool, i am able to send a WM_CLOSE message which closes the dialog of my app. And WM_QUIT hits my function int WxSyncApp::OnExit() , resulting in a clean exit of the app.
But, when i send WM_ENDSESSION, it does not hit WxSyncApp::OnAppEndSession at all. Anything wrong with the event handling code above?

Re: App exit seamlessly when system powered off

Posted: Fri Aug 23, 2019 9:25 am
by doublemax
The code looks ok to me. Try the "restart manager" tool:
https://stackoverflow.com/a/2673800

(in the comments is a link to web.archive where you can still download it)

Re: App exit seamlessly when system powered off

Posted: Fri Aug 23, 2019 12:23 pm
by deepti
Thanks a ton @doublemax! Tried with the "restart manager" tool and the app exited fine with code sample you provided below.
Just hope that it is the same message that Windows 10 actually uses too :-D
Because i see that it is a very old tool.. so, hoping that the message which Windows 10 sends on system shutdown is still the same.
Also, this is applicable only for Windows, or even other OSes?

Re: App exit seamlessly when system powered off

Posted: Fri Aug 23, 2019 12:44 pm
by doublemax
Tried with the "restart manager" tool and the app exited fine with code sample you provided below.
Did you also make a cross-check with your old code, and a "real" shutdown process?
Also, this is applicable only for Windows, or even other OSes?
Searching the wxWidgets sources, OSX seems to generate these events, too. But not GTK.

Re: App exit seamlessly when system powered off

Posted: Fri Aug 23, 2019 5:59 pm
by deepti
Did you also make a cross-check with your old code, and a "real" shutdown process?
Yes, checked with "real" system shutdown.. it doesnt crash the app.
But honestly, I have never seen it crashing even WITHOUT the new EVT_END_SESSION handler. It was reported by a customer of our app.
So, i cannot be 100% sure whether this will work in reality!