milan m
milan m

Reputation: 2254

Is there any way to capture application exit event in vb.net windows form system?

In C#, I used to capture the application exit event with the following code in program.cs.

   static void OnProcessExit(object sender, EventArgs e)
    {
        string message = "The application was closed at :" + DateTime.Now.ToString() + "."; 


    }

What is the equivalent of this in VB.NET?

I want to close the database connection if it is open by any chance in the application exit event.

Upvotes: 3

Views: 7398

Answers (2)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28413

Try this

AddHandler Application.ApplicationExit, AddressOf OnApplicationExit

Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
    ' When the application is exiting, write the application data to the 
    ' user file and close it.
    WriteFormDataToFile()

    Try 
        ' Ignore any errors that might occur while closing the file handle.
        userData.Close()
    Catch 
    End Try 
End Sub

Source

Upvotes: 3

OneFineDay
OneFineDay

Reputation: 9024

Double click on MyProject and click the Application Tab and then click the View Application Events button. There is an event for Shutdown - which fires when the application is closing.

Upvotes: 3

Related Questions