473,387 Members | 1,517 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,387 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 4378
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.