How to control the close window event

Hello

I have created a desktop app which is running on Windows, Mac and Linux. There is a dialog window which I would like to have closed in a controlled way: when the user clicks the close button of the window I want to execute a shell command in the background. When that is completed the window can be closed. Atm it looks like the dialog window gets closed and aborts the shell command right away on closing the windows. The “Shell” which executes the command is part of the dialog window to be closed and should remain there.

As workaround I have disabled the close button of the dialog window and created a separate “Close” button inside the window. After the shell command is done it closes the window programmatically. That works fine but I would like to enable the official window close button.

I’ve noted the “cancel close” event on the window that might help to find a solution but there is no explanation/sample how it works.

Anyone knows of a proper solution to use the window close button?

You can return true to the CancelClose event to, well, cancel the close. :grin:

2 Likes

If it’s a “Please wait, processing” progress dialog you could disable the close button as a simple approach.

Edit: Forgot you can’t change that at runtime (since you’re using it to activate the shell process).

Thank you for the hint. I was confident to find a solution with CancelClose event. It works very well with a separate button as I have in my workaround solution but it doesn’t work with the official close button of the window. The CancelClose event will also be fired BUT it doesn’t stop the window from closing. That way it also stops my Shell process.

Ha! Found a working solution using the CancelClose event. I would like to share in case someone else is in a similar situation.

I have created a public property variable (BOOLEAN) and set to TRUE by default. In the CancelClose event there is the IF IS TRUE THEN call the Shell process. At the end of the Shell process (complete event) I set the public property to FALSE and then trigger the mywindow.close to head into the CancelClose event again. This time it enters the ELSE condition: return FALSE which leads to the closing window.

Not quite true and there I struggled a lot: When you click the window close icon the CancelClose event is triggered BUT then the public property (by default set to TRUE) is counted as FALSE or just in an invalid state (as the window is in closing process) and the window will be closed right away (without finishing the Shell process). The trick making it work was to move the public property to the main window of the application. That way it’s always present even when the window to be closed will be closed. I just have to reset the the public property before re-opening the affected window again.

And there is no defined/used Close event in the affected window, only CancelClose is needed.