[Last Call] Learn how to a build a cloud-first strategyRegister Now


Detecting Console Application Closing...

Posted on 2006-04-25
Medium Priority
Last Modified: 2012-08-14
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!
Question by:DiamonDogX
  • 3
  • 2
LVL 23

Expert Comment

ID: 16534566
Dont think it is, i will confirm but i doubt it
LVL 23

Expert Comment

ID: 16534926
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...");

LVL 11

Expert Comment

ID: 16536260
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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 11

Expert Comment

ID: 16536287
here is an example, how a windows hook works.

Author Comment

ID: 16538331
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...
LVL 11

Accepted Solution

vo1d earned 200 total points
ID: 16541191
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.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question