Reputation: 2254
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
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
Upvotes: 3
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