S
shiry
Hi,
I need to do some important cleanup before my console application
exists. 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
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/dllproc/base/handlerroutine.htm),
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.
I need to do some important cleanup before my console application
exists. 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
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/dllproc/base/handlerroutine.htm),
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.