471,354 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

End task does not fire console CTRL_CLOSE_EVENT

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.Consol eEvent 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(MyProce ssExitHandler);

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

}

Any ideas?
Thanks.

Dec 21 '05 #1
2 4250
On 21 Dec 2005 06:19:55 -0800, "shiry" <sh****@gmail.com> wrote:
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
Any ideas?
Thanks.


--

The ultimate truth is that there is no ultimate truth
Dec 28 '05 #2
Should work, make sure you stop the task from the Applications list, not the
process from the Process list.!

Willy.

"shiry" <sh****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.Consol eEvent 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(MyProce ssExitHandler);

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

}

Any ideas?
Thanks.

Dec 29 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by A. Lovhaug | last post: by
2 posts views Thread by nick | last post: by
2 posts views Thread by Jim Heavey | last post: by
TRScheel
1 post views Thread by TRScheel | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.