Hi Gastón;
This seems like an easy thing to do when you first think about it but it is actually a very complex issue behind the scenes. Also, not all Apps are MDI and even if they are - there could also be both Child and Pop-up windows open to consider as well. Then you have another important issue ... closing the windows in the exact reverse order that they were opened. I found out a long ago that closing them in a random order can lead to App crashes. Apps also need to handle Window closes in a nice logical way too for the CloseQuery events so that business end-of-transaction states are upheld. Another reason for closing window classes in the exact reverse order.
I solved these challenges a few decades ago in my STD Framework by creating a Window Manager service. This service (which is completely transparent to any PB App) handles the tracking of opening and closing windows. It is called when any window activity is performed and also handles the Close All Windows functionality. For example:
The Window Manager service also keeps track of all MS-Window resources that the Open Window class consumes including memory and GDI resources for all of its controls. It also tracks how many times any given window is activated / deactivated, number of key stokes it receives, O/S messages and the state of the Window and its controls on disposition. For example:
If you are interested, the framework that I use for all my App development (and many PB customers do as well) - it is free and open sourced under an Apache 2.0 licensing model since 1995 (and written in 1992 before the PFC). You can read about the latest release and download it from this posting: http://chrispollach.blogspot.com/2020/08/2020r2.html
If you download the framework or better yet, it's example OrderEntry Demo App - have a look at the Window Controller object class "wn_controller_master" in the STD_FC_PB_Base.pbl and then look at the following method implementations: of_get_all_tracked_windows, of_register_tracked_window and of_unregister_tracked_window. For the Close All functionality, have a look at the Window Controller class "wn_controller_master" in the STD_FC_PB_Base.pbl library and it's "oe_close_all" event.
This code will show you how its done (or at least how I did it). You are welcome to use the framework or assimilate whatever code bits and pieces that you need into your own implementation. The above code is now in its 3rd iteration since I wrote it originally circa 1998 and the last major revision was in 2011 (with small tweaks after that). It has stood up to all the Sybase, SAP & Appeon releases of PB - including PowerServer Web & Mobile Apps (as translated to jScript). It currently works great with the new PowerClient feature based Apps found in the PB 2019 R3 beta that I have tested thus far. I hope to be testing this code soon too in the upcoming PB / PowerServer 2021 releases coming in 2021 as well. ;-)
HTH
Regards ... Chris