Solved

Handling application exit event

Posted on 2006-11-15
2
717 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …

734 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