The correct way of doing this is to include your cleanup code in a method that is being called in your FormClosing
event handler (nothing ugly in that, in my opinion). Calling Application.Exit
or closing the application the old-fashioned way then results in this event being generated. Which triggers the cleanup method.
private void Clicked(object sender, EventArgs e)
{
Application.Exit();
}
private void FormClosing(object sender, CancelEventArgs e)
{
Cleanup();
}
private void Cleanup()
{
// do cleanup here
}