Solved

Handling application exit event

Posted on 2006-11-15
2
714 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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