By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

Detecting Windows Shutdown in Console Application -- How?

P: n/a
I have a console application that spawns off several C# threads. It
needs to react to shutdown of the system by performing some end of
program activities including writing some files and closing some other
open files and then shutting down gracefully.

I've done a lot of searching online and there seem to be a lot of
potentially interesting leads, an OnShutdown is mentioned, as is a
SessionEnding event, but I'm not sure what the normal procedure is. I
suspect I need to create an event delegate and then register for this
session ending event, but I'm not exactly sure how to do it and I was
hoping to find a code sample. Does anyone know where I can find one?
Thanks.

David W. Griffin
Lockheed Martin Aeronautics Company

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You can't use these events from Console applications.
SessionEnding/SessionEnded can only be used in windows applications while
OnShutdown is for Services only. Note that as far as I know there is no way
to keep a cmd shell running once the shutdown activity is started and the
system kill's the (cmd) process.

Willy.
"David Griffin" <ca***********@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a console application that spawns off several C# threads. It
needs to react to shutdown of the system by performing some end of
program activities including writing some files and closing some other
open files and then shutting down gracefully.

I've done a lot of searching online and there seem to be a lot of
potentially interesting leads, an OnShutdown is mentioned, as is a
SessionEnding event, but I'm not sure what the normal procedure is. I
suspect I need to create an event delegate and then register for this
session ending event, but I'm not exactly sure how to do it and I was
hoping to find a code sample. Does anyone know where I can find one?
Thanks.

David W. Griffin
Lockheed Martin Aeronautics Company

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2

P: n/a
David:

Check out the Win32 call SetConsoleCtrlHandler. This may help.

John
Willy Denoyette [MVP] wrote:
You can't use these events from Console applications.
SessionEnding/SessionEnded can only be used in windows applications while
OnShutdown is for Services only. Note that as far as I know there is no way
to keep a cmd shell running once the shutdown activity is started and the
system kill's the (cmd) process.

Willy.
"David Griffin" <ca***********@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a console application that spawns off several C# threads. It
needs to react to shutdown of the system by performing some end of
program activities including writing some files and closing some other
open files and then shutting down gracefully.

I've done a lot of searching online and there seem to be a lot of
potentially interesting leads, an OnShutdown is mentioned, as is a
SessionEnding event, but I'm not sure what the normal procedure is. I
suspect I need to create an event delegate and then register for this
session ending event, but I'm not exactly sure how to do it and I was
hoping to find a code sample. Does anyone know where I can find one?
Thanks.

David W. Griffin
Lockheed Martin Aeronautics Company

*** Sent via Developersdex http://www.developersdex.com ***


Nov 17 '05 #3

P: n/a
On 2005-07-06, David Griffin <ca***********@yahoo.com> wrote:
I have a console application that spawns off several C# threads. It
needs to react to shutdown of the system by performing some end of
program activities including writing some files and closing some other
open files and then shutting down gracefully.

I've done a lot of searching online and there seem to be a lot of
potentially interesting leads, an OnShutdown is mentioned, as is a
SessionEnding event, but I'm not sure what the normal procedure is. I
suspect I need to create an event delegate and then register for this
session ending event, but I'm not exactly sure how to do it and I was
hoping to find a code sample. Does anyone know where I can find one?
Thanks.

David W. Griffin
Lockheed Martin Aeronautics Company

*** Sent via Developersdex http://www.developersdex.com ***


As was mentioned, you need to use the win32 SetConsoleCtrlHandler api to
set up a callback. The code looks something like:

using System;
using System.Runtime.InteropServices;

private enum ControlEventType
{
CtrlCEvent = 0,
CtrlBreakEvent = 1,
CtrlCloseEvent = 2,
CtrlLogoffEvent = 5,
CtrlShutdownEvent = 6,
}

private delegate bool HandlerDelegate (ControlEventType dwControlType);

[DllImport ("kernel32", SetLastError=true)]
private static extern bool SetConsoleCtrlHandler (
HandlerDelegate HandlerRoutine,
bool Add);
public class ConsoleApp
{
private static HandlerDelegate controlHandler;

public static void Main ()
{
// create the delegate and add the handler
controlHandler += new HandlerDelegate (ControlHandler);
SetConsoleCtrlHandler (controlHandler, true);

// do you cool stuff

// clean up...
SetConsoleCtrlHandler (controlHandler, false);
}

private static bool ControlHandler (ControlEventType controlEvent)
{
// if you handle the signal, return true - else false
switch (controlEvent)
{
case ControlEventType.CtrlCEvent:
....
break;
case ControlEventType.CtrlShutdownEvent:
...
break;

...
}
}
}

Anyway, this is obviously untested air code - but I do have a working
sample somewhere, if you really need it I'll dig it out. I actually
have a class with most of the win32 console api calls in it :)

--
Tom Shelton [MVP]
Nov 17 '05 #4

P: n/a

<jp******@mvisiontechnology.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
David:

Check out the Win32 call SetConsoleCtrlHandler. This may help.

John


That's right you can install your own handler using PInvoke, v2.0 makes it a
bit easier to do using the added Console support.

Thanks,
Willy.
Nov 17 '05 #5

P: n/a
Thanks. It's hard to even know what to look for -- what term to search
for. It seems like something every well behaved application would want
to do but none of my books seem to cover this and I've been less than
successful on line too. Hopefully this will steer me in the right
direction.

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.