Solved

Handling application exit event

Posted on 2006-11-15
2
707 Views
Last Modified: 2011-10-03

 Hi,
   I am developing a windows MDI appln using vb.net 2003.
   Is there anyway to handle the applicatioin.exit event . My reason is under any circumstance    when the appln is ended (possibly inculding from the task manager), I need to run a final routine to set some registry values.  How can I do this ?

  thanking you
  Sam
0
Comment
Question by:sgs1970
2 Comments
 
LVL 13

Accepted Solution

by:
newyuppie earned 125 total points
ID: 17947259
read this MSDN documentation:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.applicationexit.aspx

basically declare something like this:
' Handle the ApplicationExit event to know when the application is exiting.
    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
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17947276
AddHandler Application.ApplicationExit, AddressOf OnApplicationExit

Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
    ' ...
End Sub

However, I don't think that this function will be called when application is killed by Task Manager from Processes tab. Task Manager doesn' give any chance to application to execute any code.

There are two other places to make critical operations - this doesn't cover all possible situations for 100%, but can improve your program. If program has main window, handle its Closing event. This allows to execute some code when program is closed by user and from Applications tab of Task Manager.  First is tries to close program by "gentle" way, and only then kills it.
Second place is SystemEvents.SessionEnding Event. Add handler to it, it will be called when Windows shuts down or logs off while your application is running.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now