Search code examples
wxwidgets

How do I quit a wxWidget application when no window was opened?


I have written a wxWidget gui application. Depending on the parameters, a gui is displayed in some cases, but when the program is run in silent mode, it will just perform it's task without opening a window.

I could return false in my App::OnInit() override, and the application will terminate, but then the exit code is also set to indicate that something went wrong.

So how do I properly exit in such a case, resp. how can I set the returncode?


Solution

  • If you enter the main event loop, as it happens if you return true from YourApp::OnInit(), you must exit it to terminate the application. This is done using ExitMainLoop() which is usually called when the last top level window is deleted, but can also be called manually.

    If you don't enter the main loop event at all, then returning false from YourApp::OnInit() is the simplest way to exit the program immediately but, as you already know, this indicates a failure to initialize the application, from wxWidgets point of view, and so by default the program exits with non-zero exit code. To return your own exit code, continue to return true from OnInit() and override OnRun(), which is called next if OnInit() succeeded, and simply return the code from there, without doing anything, especially not calling the base class version which would enter the main event loop.