Is there any way to capture application exit event


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.


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



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.


  • IE9 javascript sort order … why?
  • Is there any way to capture application exit event
  • Check if a given path is a special folder path?
  • How can I break selecting when condition is false?
  • Java: can you change the value of the same string every time it loops?
  • How to Implement DialogBox and onLongClick at the same time on the button?
  • Warning - Initialization makes pointer from integer without a cast
  • CountDownTimer trivia game score - Android (java)
  • How to update powerpivot pivot table filter via cell reference?
  • VBScript to check for open process by user
  • UML diagram generator in Visual Studio 2010
  • Unable to run SDL program in Eclipse but able to do so in Windows Explorer
  • Converter from SAT to 3-SAT
  • Excel VBA How to populate a multi-dimensional (3d) array with values from multiple excel ranges?
  • C# fibonacci function returning errors
  • Debugging VB6 Code From Visual Studio 2010
  • Extract zip entries to another Zip file
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • ActiveRecord query for a count of new users by day
  • Switching to Release Build causes runtime error in Web Reference
  • NHibernate Validation Localization with S#arp Architecture
  • How can I send an e-mail from a vbs script
  • Illegal mix of collations for operation for date/time comparison
  • Azure Cloud Service Web Role web pages do not load
  • Optimizing database types to compact database (SQLite)
  • Accessing IRQ description array within a module and displaying action names
  • Get object from AWS S3 as a stream
  • TFS: Get latest causes slow project reloading
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • Running a C# exe file
  • Javascript convert timezone issue
  • Do I've to free mysql result after storing it?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Getting Messege Twice Using IMvxMessenger
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Reading document lines to the user (python)
  • How can I use threading to 'tick' a timer to be accessed by other threads?