Quitting App with Window.Close Button

I am designing a Mac application that has a single window. The Close Button has been enabled. The desired behavior is that if the user clicks on the Close Button the application quits.

New in 2019r2, there is an Application property (AllowAutoQuit) that on Macs defaults to False. Under this circumstance, if the user clicks on the Close Button, the window goes away but the application continues to run. If you set this property to True (say in the App Open Event) then if the user clicks on the Close Button on the last window (in my case the only window) the application will quit.

In a previous application, I had done the above and the application quit when the window closed. Then I managed to forget about the AllowAutoQuit property, and I was trying to accomplish the same behavior. I could not figure out how I had accomplished this before.

Finally, as an alternative approach, I had placed the command Quit in the Close Event of the window. That accomplished the same effect as best I could tell.

Eventually, I figured out how my previous application had managed to quit.

But now this raises a question in my mind. Is there some reason that one approach is preferable to the other?

Not in a single window app, but in a multiple-window app, Auto-quit is preferred. Otherwise you’d have to figure out yourself if it’s time time quit on Close.

1 Like

Thanks Kem

There might be a case where you have multiple document windows open, where some of them are just windows that let the user manage properties, or something like that.
With the AutoQuit option, the app won’t quit until all the windows are closed.

In that case you can have an array or dictionary, that hold references to the project windows only.
When closing a project window you iterate through the references. When the reference of the closing window is found, remove the reference. When no references are left, you can quit the app

2 Likes

Sometimes it is worth to make sure all windows will get closed when you close the main window. Put this in main window close event and it will take care about closing all opened windows at once:

For i As Integer = WindowCount - 1 DownTo 0
Var w As Window = Window(i)
If w <> Nil then w.close
Next

2 Likes

I would add code in there to NOT close the window that’s calling this function, otherwise you could end up in a loop.

1 Like

I am doing this on main window in close events in many of my projects and - no loop since I am doing it on main window, which is the last one to close. I found that on MacOS 64bit apps when using quit - while several windows were still shown - crashed a lot. I did this to remedy the situation - no more crashes.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.