Solved

Handling application exit event

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

17 Experts available now in Live!

Get 1:1 Help Now