I would like to know if there is a way that when you exit a console app pressing the X button, the program makes an action.
I know there is a way with Ctrl + C:
CancelKeyPress += new ConsoleCancelEventHandler(MyHandler);
while (true)
{
//The program code...
}
protected static void MyHandler(object sender, ConsoleCancelEventArgs args)
{
//Action
}
But I need one that work with the exit button or/and alt + F4.
Thanks for your time.