Detecting Console Application Closing...

In C#, I'm trying to figure out a way to perform an action (some piece of code) when the user closes a console application (note: NOT a GUI application), i.e. when the "X" on the console window is clicked to close the window.  Is this possible?  Thanks!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dont think it is, i will confirm but i doubt it
Had a look, can't find anything.

the closest i came was using a destructor but that only fired when the console app terminates properly:

         Console.WriteLine("Shutting down, please wait...");

you can use the processexit event of the current appdoamin to check, if your console app is going to shutdown.

AppDomain appDomain = AppDomain.CurrentDomain;
appDomain.ProcessExit += new EventHandler(appDomain_ProcessExit);

but this wil not safe your app to be closed via the 'x' button or windows message.
for this, i would recommend a windows hook to prevent, that the WM_CLOSE message will be send to the console window, where your app is running.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

here is an example, how a windows hook works.
DiamonDogXAuthor Commented:
That link has code that appears to only work for System.Windows.Forms applications.  Still having problems determining when that console window is being closed... tried various C# functions such as the AppDomain one you described above.  Doesn't seem to be possible unless you have some other program/process monitoring your other one...
the example is for a windows form, but if you use the win32 api functions via pinvoke you can use it in your application.
you will have to use the findwindow function to get a handle to your console window and then you could trap the wm_close message for that window.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.