Skip to main content

Application overview

An xtd GUI application must use the xtd::forms::application class to run process event loop.

The following code shows the minimum required to run a GUI application. The application will exit automatically when the form is closed.

#include <xtd/xtd>

auto main()->int {
xtd::forms::application::run(xtd::forms::form {});


The xtd::forms::application class provides static methods and properties to manage an application, such as methods to start and stop an application, to process Windows messages, and methods to get information about an application. The xtd::forms::application class is a static class and cannot be inherited.

Start process event loop

The following methods are used to start process event loop:

The following code shows the use of the application::run() method without parameter. Unlike the method with the specified form parameter, the message loop application will not stop when you close the window. You have to call the method applicati::exit or application::exit_thread explicitly.

#include <xtd/xtd>

using namespace xtd::forms;

auto main()->int {
auto form1 = form {};;
form1.form_closed += [] {
// If you comment the following line the application will not exit when you close the form.


Stop process event loop and exit

The following methods are used to stop process event loop and exit:


The application class contains some global settings that must be called before executing the event loop.

Application context

xtd::forms::application_context class specifies the contextual information about an application thread.

The xtd::forms::application_context contains or not the main form. You can set or change a main form in runtime with the xtd::forms::application_context::main_form property.

The following code shows how to create an application_context, set a form as main_form and call appplication::run with it.

#include <xtd/xtd>

using namespace xtd::forms;

auto main()->int {
auto form1 = form {};
form1.text("form 1;");;

auto form2 = form {};
form2.text("form 2");

auto context = application_context {};


form 2 is the main window. If you close form 1, the application continues to run and form 2 remains visible. On the other hand, if you close form2, the application is exited and form 1 is closed. Note that you can change the main form at any time while the application is running

See also