Jacob Manaker
Jacob Manaker

Reputation: 730

VB .NET application not exiting when all forms close

I wanted to create an application with two forms, a main one called Source and a subsidiary called Viewer. Using buttons on each form, the user could switch to the other, even if that form had previously been closed. So I set my program to Close When Last Form Closes. Moreover, I added message boxes when each form closed, just to check that the user was sure.

But now my application won't exit after I close both forms! It shows up in Task Manager, and if I run it in MSVS, the debugger never stops! When each form is closed, My.Application.Forms returns an empty collection. And if I force quit it using End/Application.Exit, the program still quits — see the MWE below. What do I do?

MWE

Create a blank VB.NET Windows Forms project. In the "Application" tab of your project's settings, choose "Close When Last Form Closes." In the designer, create a Form Source with one button.

Public Class Source

Public Sub Switch(sender As Object, e As EventArgs) Handles Button1.Click
    My.Forms.Viewer.Show()
End Sub

Public Sub Free(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
    e.Cancel=(MsgBoxResult.Cancel=MsgBox("Are you sure?"))
End Sub

'Public Sub Test(sender As Object, e As EventArgs) Handles Me.Closed
    'Debug.Assert(Not My.Application.Forms.Count))
    'If the next line is uncommented, the application will close, like we want
    'If Not My.Forms.Viewer.Visible Then Application.Exit()
'End Sub

End Class

Then create an identical form called Viewer.

Public Class Viewer

Public Sub Switch(sender As Object, e As EventArgs) Handles Button1.Click
    My.Forms.Source.Show()
End Sub

Public Sub Free(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
    e.Cancel=(MsgBoxResult.Cancel=MsgBox("Are you sure?"))
End Sub

'Public Sub Test(sender As Object, e As EventArgs) Handles Me.Closed
    'Debug.Assert(Not My.Application.Forms.Count))
    'If the next line is uncommented, the application will close, like we want
    'If Not My.Forms.Source.Visible Then Application.Exit()
'End Sub

End Class

Try closing any form last. Neither will cause the app to quit.

Upvotes: 2

Views: 1546

Answers (1)

Jacob Manaker
Jacob Manaker

Reputation: 730

Don't use Form.Closing and Form.Closed

In MSDN's page on Form.Closing, it remarks:

Caution

The Closing event is obsolete in the .NET Framework version 2.0 [and above]; use the FormClosing event instead.

The events were deprecated because

The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

The MSDN documentation nowhere states this, but the reverse is true: in order for your form to signal to the VB.NET runtime that the form closed, Form.Closing and Form.Closed cannot be subscribed to.

If you replace Me.Closing with Me.FormClosing and Me.Closed with Me.FormClosed in your MWE, it works.

Upvotes: 1

Related Questions