Detecting Windows Shutdown in Console Application -- How?

  • Thread starter Thread starter David Griffin
  • Start date Start date
D

David Griffin

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
 
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.
 
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

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 :)
 
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.
 
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.
 
Back
Top