End task does not fire console CTRL_CLOSE_EVENT

S

shiry

Hi,

I need to do some important cleanup before my console application
exists. I got some ideas from the groups and I used the console ctrl
event. This is working well and it fires for all cases, including the
CTRL_CLOSE_EVENT (if I close the application from the X botton). But on
the contrary to what the MSDN indicates, ending my process from the
task manager did not fire the CTRL_CLOSE_EVENT.
I also tried to use the Appdomain processexit, which didn't work for
any of the cases.

This is the code as I tried it:

public enum ConsoleEvent
{
CTRL_C = 0,
CTRL_BREAK,
CTRL_CLOSE,
CTRL_LOGOFF = 5,
CTRL_SHUTDOWN
}

public delegate void ControlEventHandler(ConsoleEvent consoleEvent);

public event ControlEventHandler ControlEvent;

ControlEventHandler eventHandler;

public ConsoleCtrlEventsSetup()
{
// save this to a private var so the GC doesn't collect it...
eventHandler = new ControlEventHandler(Handler);
SetConsoleCtrlHandler(eventHandler, true);
}

~ConsoleCtrlEventsSetup()
{
Dispose(false);
}

public void Dispose()
{
Dispose(true);
}

void Dispose(bool disposing)
{
if (disposing)
{
GC.SuppressFinalize(this);
}
if (eventHandler != null)
{
SetConsoleCtrlHandler(eventHandler, false);
eventHandler = null;
}
}

private void Handler(ConsoleEvent consoleEvent)
{
if (ControlEvent != null)
ControlEvent(consoleEvent);
}


[DllImport("kernel32.dll")]
static extern bool SetConsoleCtrlHandler(ControlEventHandler e, bool
add);

}


class CrashHandler
{
public static void
MyProcessExitHandler(ConsoleCtrlEventsSetup.ConsoleEvent consoleEvent)
{

StopAllMotions();
}

public static void StopAllMotions ()
{
StreamWriter y = File.AppendText(@"c:\temp\test.txt");
y.WriteLine("stopping all motions");
y.Flush();
y.Close();
}
}

public static void Main()
{
ConsoleCtrlEventsSetup cc = new ConsoleCtrlEventsSetup();
cc.ControlEvent += new
ConsoleCtrlEventsSetup.ControlEventHandler(MyProcessExitHandler);

Console.WriteLine("Press any key to exit");

}

Any ideas?
Thanks.
 
R

rossum

Hi,

I need to do some important cleanup before my console application
exists. I got some ideas from the groups and I used the console ctrl
event. This is working well and it fires for all cases, including the
CTRL_CLOSE_EVENT (if I close the application from the X botton). But on
the contrary to what the MSDN indicates, ending my process from the
task manager did not fire the CTRL_CLOSE_EVENT.
I also tried to use the Appdomain processexit, which didn't work for
any of the cases.

This is the code as I tried it:
[snip code]

Am I being a bit too obvious? Have you tried try ... finally?

public static void Main() {
try {
// Normal run code here
}
finally {
// Clean up code here
}
}

HTH

rossum
 
W

Willy Denoyette [MVP]

Should work, make sure you stop the task from the Applications list, not the
process from the Process list.!

Willy.
 

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