Managing an emergency shutdown

You can't always close your program in a controlled way. Sometimes, you need to end the application immediately and you don't care that your program cleans up after itself fully. For example, a critical resource may have closed or become corrupted. If the system is shutting down, you may not be able to do all the cleanup.

There are two mechanisms for exiting your wxPython application in an emergency situation. You can call the wx.App method ExitMainLoop(). This method explicitly causes the main message loop to break, causing the control to leave the MainLoop() function. This will generally end the application—it's effectively equivalent to closing all the top-level windows.

You can also call the global method wx.Exit(). Neither method is recommended for normal use because it may cause some cleanup functions to be skipped.

Sometimes, your application will need to shut down due to an event outside of its control. An example of this is when the underlying operating system is about to shut down or log the user off. In that case, your application gets one shot at cleanup to save documents or close connections or whatever. If your application binds an event handler for the wx.evt_query_end_session event, then that event handler is called when wxPython is notified of the shutdown. (We'll show how to bind events to handlers later in the chapter, and in more detail in chapter 3.) The event parameter is a wx.CloseEvent. The close event may allow the application to veto the shutdown. Use the event method CanVeto() to find out. The application can make its veto known by calling the event method Veto(). You might want to do this if you cannot successfully save or close all resources. The default handler for the wx.EVT_QUERY_END_SESSlON event calls the Close() method of the top-level windows, which will in turn send the wx.EVT_CLOSE event to the top-level windows giving you another chance to control the shutdown process. If any of the Close() methods returns False then the application attempts to veto the shutdown.

Was this article helpful?

0 0

Post a comment