Greylander
Greylander

Reputation: 1826

startup form.close and application.exit not terminating app

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
            Forms.Application.Exit()
        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.

Upvotes: 0

Views: 149

Answers (3)

KeithS
KeithS

Reputation: 71591

You just need to break out of the loop. As other answers have thoroughly covered, Application.Exit() signals the application to close, but it lets all application threads finish what they were doing, so while you remain in that loop your main thread will never finish.

Several ways to break out of this loop; the most proper would be to exit the while statement, or exit the entire function.

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
        Forms.Application.Exit()
        Exit While 'or Exit Sub
    End If
    options.ShowDialog() 'this line can be commented
End While

Upvotes: 1

Travis
Travis

Reputation: 699

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.

Upvotes: 2

CodeCaster
CodeCaster

Reputation: 151720

Application.Exit() simply tells Windows your application wants to quit, and cleans up some resources. It doesn't stop your code, so your loop keeps on running.

You are responsible for letting all threads stop. You can indeed do that by adding an Exit Sub. Or some of the Environment methods, like Environment.Exit() or Environment.FailFast(), but both are overkill and you in your case would only be using them to hide bad design. Just exit the loop.

Upvotes: 1

Related Questions