Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Handling application exit event

Posted on 2006-11-15
2
Medium Priority
?
734 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 375 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 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