start.close and application.exit form that does not terminate the application


I have the following loop in the .load even handler for that startup form of an application:

        While (Not Directory.Exists(My.Settings.pathToHome))
        Dim response As MessageBoxResult = Forms.MessageBox.Show("Some message","Some Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
        If response = MessageBoxResult.Cancel Then
            Me.Close() 'can comment this line or next or leave both uncommented
        End If
        options.ShowDialog() 'this line can be commented
    End While

If the user selects "cancel" on the messagebox, can have either or both Me.Close() and Forms.Application.Exit() lines execute, but instead of the application terminating, the while loop goes infinite. This can be seen explicitly by stepping through the debugger.

The options form and the messagebox never open after the first cancel on the message box, though may see one or both of them "flicker" as the loop spins. If stepping through the debugger, I hear the "chime" from the messagebox, though it does not appear.

I imagine I could also add an "exit sub" in there. But should that be necessary, and is it reliable? It seems strange especially that application.exit does not successfully terminate the thread.

You need an Exit While after the Forms.Application.Exit.

Application.Exit still allows the threads to finish what they are doing, and since you are effectively in an infinite loop it will never actually let the application close.