Why does CancelClose get called twice when a window is closed via the close button?

In my main window, I use the CancelClose event to allow the user to save unsaved data when closing the window (either via the Quit menu or by closing the window). I then call Quit in the Close event. However, I’m seeing the CancelClose event being called again when Quit is called. The result is that my App doesn’t quit when the window’s Close button is clicked. I’ve even added a check for appQuitting at the top of the CancelClose event …

Is this expected or a bug?

Don’t call Quit in the Close event. It’s creating another attempt to close the window and it hasn’t finished closing during the Close event. That’s where the second CancelClose event is coming from. App.AutoQuit = true is the correct way to have your app quit when the last window is closed.

… except that the main window isn’t always the only window open. And, this has always worked previously dating back to 12r2.1 at least.

You can close all of the other related windows in the Close event if you need to.

In the close event cycle through all open windows and close all the others.

Just because it ‘worked’ doesn’t mean it was supposed to. :slight_smile: It might have changed because a bug was fixed that altered behavior a bit.

If closing your main window quits the app then I would check for the AppQuitting parameter to see if it’s true. If it’s true you know it came from the app trying to quit and you can see if you’ve saved changes (returning true keeps it from quitting). If it’s false it’s your window attempting to close and you can call Quit. It’s up to you managing it via the AppQuitting parameter.

I think it’s a tricky way of doing it but I feel it’s possible.

That’s exactly what I do now.

I’ll try stepping through the open windows and close them first and see where that leaves things.

Heads up, 18r4 has some internal changes on Windows in regard to the Window() function. Collect them into an array, then iterate through the array to close them.

Or use calllater to call quit which will give the os time to finish the original close and get you out of the close method.

The lazy way out always bites you down the road.

I’m not lazy, I will just do it tomorrow :stuck_out_tongue: