473,222 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 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 4371
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: A. Lovhaug | last post by:
I have a console application built in the .NET Framework. This application basically executes an XCopy based on parameters that I pass to it. I use it for creating scripts for backing up folders,...
2
by: David Olive | last post by:
Hi guys, I'm having a bit of a problem getting a VB .NET console app to run happily as a scheduled task. The app itself generates a bunch of word documents on a file share on another server by...
2
by: mark4asp | last post by:
Why does this not work in Mozilla ? <http://homepage.ntlworld.com/mark.pawelek/code/animals.html> The optHabitat_change() event does not fire. What am I doing wrong here? PS: It should...
2
by: nick | last post by:
I am using C# do write a console program. Can I do the following tasks: 1. Detect if a key on keyboard is pressed? and get the typed ASC II code? 2. Catch and handle the event of the Control-C?...
2
by: Jim Heavey | last post by:
When I run a console application by bringing up a dos window and then running the exe, I noticed that when I close the dos window, this kills my application. My question is, Is there some way that...
6
by: carbon_dragon | last post by:
Ok, so here is the problem. I'm working on a headless server program implemented as a .NET C# Console project. There is a UPS mounted to this server (though not a windows compliant UPS). I can only...
10
by: shiry | last post by:
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...
1
TRScheel
by: TRScheel | last post by:
Here's a good one. I have spent all day researching this, and I can not for the life of me find a method that will work. All I want to do... is add a scheduled task to the Task Scheduler. I...
9
by: jdaelhousen | last post by:
I have a bit of a problem I'm hoping someone can shed some light on... I have a VB.Net console application written in VS 2003 that produces a .exe file that now sits on a Windows 2000 server...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.