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_EVEN T (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.2003 OCT.1033/dllproc/base/handlerroutine. htm),
ending my process from the task manager did not fire the
CTRL_CLOSE_EVEN T.
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 ControlEventHan dler(ConsoleEve nt
consoleEvent);
public event ControlEventHan dler ControlEvent;
ControlEventHan dler eventHandler;
public ConsoleCtrlEven tsSetup()
{
// save this to a private var so the GC doesn't
collect it...
eventHandler = new
ControlEventHan dler(Handler);
SetConsoleCtrlH andler(eventHan dler, true);
}
~ConsoleCtrlEve ntsSetup()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
void Dispose(bool disposing)
{
if (disposing)
{
GC.SuppressFina lize(this);
}
if (eventHandler != null)
{
SetConsoleCtrlH andler(eventHan dler,
false);
eventHandler = null;
}
}
private void Handler(Console Event consoleEvent)
{
if (ControlEvent != null)
ControlEvent(co nsoleEvent);
}
[DllImport("kern el32.dll")]
static extern bool
SetConsoleCtrlH andler(ControlE ventHandler e, bool
add);
}
class CrashHandler
{
public static void
MyProcessExitHa ndler(ConsoleCt rlEventsSetup.C onsoleEvent consoleEvent)
{
StopAllMotions( );
}
public static void StopAllMotions ()
{
StreamWriter y =
File.AppendText (@"c:\temp\test .txt");
y.WriteLine("st opping all motions");
y.Flush();
y.Close();
}
}
public static void Main()
{
ConsoleCtrlEven tsSetup cc = new
ConsoleCtrlEven tsSetup();
cc.ControlEvent += new
ConsoleCtrlEven tsSetup.Control EventHandler(My ProcessExitHand ler);
Console.WriteLi ne("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_EVEN T (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.2003 OCT.1033/dllproc/base/handlerroutine. htm),
ending my process from the task manager did not fire the
CTRL_CLOSE_EVEN T.
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 ControlEventHan dler(ConsoleEve nt
consoleEvent);
public event ControlEventHan dler ControlEvent;
ControlEventHan dler eventHandler;
public ConsoleCtrlEven tsSetup()
{
// save this to a private var so the GC doesn't
collect it...
eventHandler = new
ControlEventHan dler(Handler);
SetConsoleCtrlH andler(eventHan dler, true);
}
~ConsoleCtrlEve ntsSetup()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
void Dispose(bool disposing)
{
if (disposing)
{
GC.SuppressFina lize(this);
}
if (eventHandler != null)
{
SetConsoleCtrlH andler(eventHan dler,
false);
eventHandler = null;
}
}
private void Handler(Console Event consoleEvent)
{
if (ControlEvent != null)
ControlEvent(co nsoleEvent);
}
[DllImport("kern el32.dll")]
static extern bool
SetConsoleCtrlH andler(ControlE ventHandler e, bool
add);
}
class CrashHandler
{
public static void
MyProcessExitHa ndler(ConsoleCt rlEventsSetup.C onsoleEvent consoleEvent)
{
StopAllMotions( );
}
public static void StopAllMotions ()
{
StreamWriter y =
File.AppendText (@"c:\temp\test .txt");
y.WriteLine("st opping all motions");
y.Flush();
y.Close();
}
}
public static void Main()
{
ConsoleCtrlEven tsSetup cc = new
ConsoleCtrlEven tsSetup();
cc.ControlEvent += new
ConsoleCtrlEven tsSetup.Control EventHandler(My ProcessExitHand ler);
Console.WriteLi ne("Press any key to exit");
}
Any ideas?
Thanks.
Comment