App exit seamlessly when system powered off

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

App exit seamlessly when system powered off

Post 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!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: App exit seamlessly when system powered off

Post by ONEEYEMAN »

Hi,
Look here.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: App exit seamlessly when system powered off

Post 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
Use the source, Luke!
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: App exit seamlessly when system powered off

Post 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!
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: App exit seamlessly when system powered off

Post 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();
}
Use the source, Luke!
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: App exit seamlessly when system powered off

Post 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?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: App exit seamlessly when system powered off

Post 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)
Use the source, Luke!
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: App exit seamlessly when system powered off

Post 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?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: App exit seamlessly when system powered off

Post 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.
Use the source, Luke!
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: App exit seamlessly when system powered off

Post 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!
Post Reply