I have Console application. How can I handle closing of the Concole window ?
Printable View
I have Console application. How can I handle closing of the Concole window ?
what do you mean?
If you're displaying text and it's closing before you can read it, then you'd just have to put a Console.Readline()
If you run your app in a console window that is already open, that window will remain open when your app exits. Likewise, if your app opened a new window in which to run that window will be closed when your app exits. In the second case, to avoid the window closing without warning the user, take MrPolite's advice, perhaps preceeded with Console.Write("Press ENTER to exit.").
I guess he/she meant the cmd.exe Window. He/She wants to check that the Windows is being closed explicitly by the user to avoid loss of information on his/her program running in console mode. I guess we don't have access to knowing this thing. Windows starts it as a thread.
Or we can get the thread ID of this one and remember it when Windows tries to kill it?
Quote:
Originally Posted by MrPolite
I have Console application. Sometimes my application prints something using Console.WriteLine. I want to handle closing of my application by user pressing
on X at top right corner of the Console window. I want to execute any code before my application closes.
well the reason the window closes is because you application has finished. If you run cmd.exe and then run your program from there, you'll notice that the window wont close by itself (becuase when your program finishes running, it returns to cmd.exe and cmd.exe is still running).
As suggested before, you can do a Console.ReadLine() at the end.
(you could do that in a while-loop also;) )
As far as I'm aware you're console app has no actual awareness of the window it's running in. If the user closes a console window using that close button before the app has finished execution then, frankly, they're an idiot and deserve to lose their data.
That's why we say "All user's are loosers".Quote:
they're an idiot and deserve to lose their data.
You got to think of users as a bunch of overactive kids that will break anything that can be broken.
One just have to keep Murphy's law in mind when developing applications.
There must be a way one can attach to a window and receive some event notification as it is closed. Before it was done with API, I'm sure. I don't have it on my machine now, but look in APIViewer if you got it.
*Ahem*
use this event (fires shortly before a program exits)...
Application.ApplicationExit()
But the Application class is a member of the System.Windows.Forms namespace, so I'm not sure that it can be used in a console app. It may be possible, but I don't think it would be in that namespace if it was intended to be used for non WinForms apps.Quote:
Originally Posted by wossname
Hmm. Seems you're right about this but I know for a fact that you can use Application.StartUpPath from within a console window because I did it the other day.
Probably because its static.
Hmm (again).
I had a quick test and you can add a reference to System.Windows.Forms to a console app with no problems, but the Application.ApplicationExit event doesn't get raised. I tried using AddHandler to attach a procedure to the event in both a WinForms app and a console app and the procedure was executed in WinForms but not the console. Presumably a WinForms app has a tighter connection to that namespace somehow.