So far this is what I’ve done. It seems to work but I’ll do more testing.
I’ve put the following code into App.CancelClose, this is the first part of the process of shutting down the application and is called before any Window.Close is activated. In there I’ve make an array of the Windows of interest (in my case CSW class). This is vital as attempting to close a window in this loop would cause Window() function to reorder, shrink or otherwise change and mess the whole process up.
I’m also testing if the window has “Changed” in this loop. If none of them have that flag you may as well just allow the shutdown to continue normally.
If I have some work to do I set App.AllowCancelClose to true, which does the job of triggering the app to quit once the final window has completed its save and closed. At this point the App.CancelClose will get called again and we must detect that there’s no work to do and allow normal shutdown (aWindows.LastIndex = -1).
Function CancelClose() Handles CancelClose as Boolean
Var nWindow As Integer
Var nWindows As Integer = App.WindowCount
Var aWindows() As CSW
Var NoneChanged As Boolean = True
For nWindow = 0 To nWindows - 1
If App.Window( nWindow ) IsA CSW Then
aWindows.Add CSW( App.Window( nWindow ) )
If App.Window( nWindow ).Changed Then
NoneChanged = False
' If none of the windows needed saving or there where no CSW windows open
If NoneChanged Or aWindows.LastIndex = -1 Then
' Then just let the application Quit In the normal way
' Set the app to automatically quit when the last window closes
App.AllowAutoQuit = True
' Now attempt to close each window
For nWindow = 0 To aWindows.LastIndex
aWindows( nWindow ).Close
The CSW window class has a CancelClose event that detects the “Changed” status and asks the usual “Don’t save, Cancel, Save” dialog. If they hit Cancel, or say save and then hit cancel on the save as dialog the App.AllowAutoClose gets set back to false. Don’t save just allows the window to close in the normal way. Clicking Save returns True from the CancelClose preventing the window from closing. The thread monitoring timer checks the App.AllowAutoClose = True and closes the window when the Save is complete.
Function CancelClose(appQuitting as Boolean) Handles CancelClose as Boolean
#Pragma Unused appQuitting
If Changed Then
Var oDialog As New CloseCancelDialog
Select Case oDialog.ShowModalWithin( Self )
' They pressed Cancel on the Closing dirty window dialog
App.AllowAutoQuit = False
' They pressed save on the Closeing dirty window dialog
If SaveTable( False, True ) Then
' They pressed the cancel button on the SaveAs Dialog so we're done with Quitting
App.AllowAutoQuit = False
' They pressed don't save, continue to close and quit if that's whats happening