0

A simple question - How to write a function that triggers on Application.Exit() ?(or whatever Application Closing method you prefer)

Keep in mind that I'm not asking for when the main form is closed or anything like that. That is done something like this:

 Private Sub AppExit(sender As Object, e As EventArgs) Handles Me.FormClosing
    msgbox("I'm closing")

End Sub

So basically, what sort of handle would I have to use ? Or, if you know a better way to do this, do tell. Thanks in advance.

1 Answers1

2

Try an AddHandler for AppDomain.ProcessExit

e.g. via Thread.GetDomain

See https://stackoverflow.com/a/16930553/2319909

Example:

Class Foo
    Private Shared Sub Main(args As String())
        AppDomain.CurrentDomain.ProcessExit += New EventHandler(AddressOf CurrentDomain_ProcessExit)
        Console.WriteLine("start")
        Console.ReadLine()
    End Sub

    Private Shared Sub CurrentDomain_ProcessExit(sender As Object, e As EventArgs)
        Console.WriteLine("Process is exiting!")
    End Sub
End Class
Community
  • 1
  • 1
Sam Makin
  • 1,526
  • 8
  • 23
  • MSDN seems broken for the AppDomain.ProcessExit event - check out the google cache: http://webcache.googleusercontent.com/search?q=cache:9wRNq44qCuEJ:https://msdn.microsoft.com/en-us/library/system.appdomain.processexit%28v%3Dvs.110%29.aspx+&cd=1&hl=en&ct=clnk&gl=uk – Sam Makin Feb 17 '15 at 16:42
  • I think this is what I am looking for, but I'm not sure how to implement it. Can you give me a runthrough or sort of a pseudocode for what I need to do? – The Revanchist Feb 17 '15 at 16:47