The Revanchist
The Revanchist

Reputation: 25

How to write a function that triggers on application exit

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.

Upvotes: 0

Views: 238

Answers (1)

Sam Makin
Sam Makin

Reputation: 1556

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

Upvotes: 2

Related Questions