Mark- 29 Posted July 30, 2021 (edited) Hello, Delphi 10.2.3, VCL Might seem simple but I have not run into this issue and have limits on what changes I can make. Form A is opened containing one button. The button opens a modal dialog, form B, displaying a message. Before the user can close form B, another part of the program posts a close message to form A and it closes. The user closes form B, leading to a crash. Setting a flag to indicate a modal dialog has been opened to prevent form A from closing is not possible. That would be a big change. Users design the screens and run script, etc. I can change the FormCloseQuery, FormClose and FormDestroy of form A. Finding what to check in the form properties/methods has produced no joy. Forest for the trees might be present. Any ideas? Thanks, Mark Edited July 30, 2021 by Mark- Share this post Link to post
Mark- 29 Posted July 30, 2021 Perhaps I can get the foreground window, check it for modal and if so prevent form A from closing in FormCloseQuery. Tomorrow. Share this post Link to post
Guest Posted July 30, 2021 Well, lets discuss your application behaviour 2 hours ago, Mark- said: another part of the program posts a close message to form A and it closes. This could be better, will be clear in few lines, but this is the root of your problem, i am talking about closing message vs closing request ! 2 hours ago, Mark- said: I can change the FormCloseQuery, FormClose and FormDestroy of form A. Sure and right way to do it, but you are deciding this within the form or (a form) not from the application as one whole creature. 2 hours ago, Mark- said: Any ideas? There is many approaches, the easiest and simplest one is to have a controller for exiting the application, by controller i mean any logic to define your application relation with its forms and messages, a controller can be a class or a hidden form (not visible) or few global functions with few global vars. The idea is to lets say have a class that will live as long as the application is not terminated, when a form like B show in modal it will register with that controller as opened and will unregister when it close, on other hand A will consult with the controller when execute CloseQuery to see if there is B or any B's like, the last thing is to change the bahaviour mentioned here "another part of the program posts a close message to form A and it closes." to "another part of the program posts close request to the controller." here the controller will have its logic on how to terminate the application and what forms need to be closed and if there is background threads working need to be waited for or terminated or.... In other word redesign your program exit code to have its own messages/events and respond according to logic your define, this will simplify your code as it will be Controller.RequestAppClose and it will close in orderly fashion, also "Controller.IsClosing : Boolean" will be nice addition if needed, the controller does know the main form and will keep track of dialogs and modal dialogs, while B doesn't need to have to handle A and A doesn't need to keep track of B. Share this post Link to post
Mark- 29 Posted July 30, 2021 (edited) Thanks for the response "Kas Ob.". This issue is not about the application closing. These form A come and go in a 24/7 program. The possible changes are limited to the areas I mentioned, in form A. The user designs/creates/codes/uses/etc. these form A. We provide the IDE and execution of the user created form A inside the main program, like an add on, along with other user configurable features. Large changes to a huge program for one issue with the form A, that was actually found by destructive testing, a user in 14 years has never created the issue, is not prudent/practical/etc. Again, thanks for the reply. Edited July 30, 2021 by Mark- Share this post Link to post
Rollo62 539 Posted July 30, 2021 I would consider closing requests by TMessage and TMessageManager, to keep all units as much decoupled as possible ( not sure if you work with a recent version of the IDE, but I think its in for long time already ). Share this post Link to post
Uwe Raabe 2064 Posted July 30, 2021 5 hours ago, Mark- said: Setting a flag to indicate a modal dialog has been opened to prevent form A from closing is not possible. The VCL already provides such a flag for you: TApplication.ModalLevel 1 1 Share this post Link to post
Mark- 29 Posted July 30, 2021 Thanks for the response "Rollo62", interesting. "Uwe Raabe" thank you. I will check it out. Share this post Link to post