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

x
?
Solved

Detecting Console Application Closing...

Posted on 2006-04-25
6
Medium Priority
?
269 Views
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!
0
Comment
Question by:DiamonDogX
  • 3
  • 2
6 Comments
 
LVL 23

Expert Comment

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

Expert Comment

by:apresto
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:

~Your_Class_Name()
{
         Console.WriteLine("Shutting down, please wait...");
         Console.ReadLine();
}

0
 
LVL 11

Expert Comment

by:vo1d
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.
0
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

by:vo1d
ID: 16536287
here is an example, how a windows hook works.
http://www.codeproject.com/csharp/NetWin32Hooks.asp
0
 

Author Comment

by:DiamonDogX
ID: 16538331
vo1d,
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...
0
 
LVL 11

Accepted Solution

by:
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.
0

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