Console App capturing close button event

M

Meya-awe

Hi,
I have a console app, i want to be able to do some clean up when the
user exits the application. So, i need to capture the red close window
button event on the "cmd.exe" window. How can i do this?

I know that i can make my app a windows app and hide the window - right
now, this is not enough of a reason to go this route. And in that case,
i would have to have the user to go to the task manager or provide
another means to shutdown the app when needed such as another utility
program or write code into the prog to check for an already running
instance and shut it down and then continue.
thanks,
BRAMOIN
 
G

Guest

Willy posted sample how to do this
http://groups.google.com/group/micr...e_frm/thread/c72efe6c358eca9e/588a7bf4cfb4832

Meya-awe said:
Hi,
I have a console app, i want to be able to do some clean up when the
user exits the application. So, i need to capture the red close window
button event on the "cmd.exe" window. How can i do this?

I know that i can make my app a windows app and hide the window - right
now, this is not enough of a reason to go this route. And in that case,
i would have to have the user to go to the task manager or provide
another means to shutdown the app when needed such as another utility
program or write code into the prog to check for an already running
instance and shut it down and then continue.
thanks,
BRAMOIN

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
M

Meya-awe

I am using C#, so i don't know how to import Kernel32 into it. in any
case, when i used the other suggestion: Binding to the process, it does
not work. Here is a simplified version of my code

namespace progName
{
class progName
{
private static progName pName = null;

public delegate void OnExited(object o, System.EventArgs e);

// other variable initializations

public progName()
{ // some object initialization }

[STAThread]
static void Main(string[] args)
{
pName = new progName();

System.Diagnostics.Process thisProc =
System.Diagnostics.Process.GetCurrentProess();

thisProc.EnableRaisingEvents = true;
thisProc.Exited += new EventHandler(pName.OnExited);

while(pName._shutDown == false)
{
thisProc.WaitForExit();
pName._shutDown = true;
}

// psuedo code ....Call other objects shutdown
}

pubic void OnExited(object o, System.EventArgs e)
{
// psuedo code ... Call other objects shutdown
}
}
}

I put a breakpoint on the first line in OnExited and it is never called
when i close the window.

BRAMOIN
 
M

Meya-awe

Correction: Binding to the process worked and the
System.Diagnostics.Process method did not work. Also, this binding to
the process method that i have mentioned is in the link suggested by
Willy:
BRAMOIN
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top