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

How do I delay Windows shutdown with a Console Project in .NET?

P: n/a
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 talk to
the UPS over a special device driver.

Through this device driver I can detect that the UPS is going to notify
Windows 2000 server to shut down. So I start doing a graceful
termination. But Windows shuts down pretty quickly and there isn't time
to close my files and gracefully shut down. Apparently, the default
console application in .NET has a default behavior of terminating
immediately -- at least that is what seems to happen.

What I need to do is to delay windows for long enough (just a few
seconds really) so that I can gracefully terminate. How do I do that
with a console project. How do I delay the time between the time that
Windows 200 Server is told to shut down and the time it actually
teminates it's applications and shuts down? I've done a lot of web
searches and .NET searches but haven't found a console solution. Maybe
all I need is a steer to the right approach.

Thanks.

Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
You can't do this with a console program. With a windows program, you
can handle the FormClosing event (new to .NET 2.0) and actually cancel the
shutdown (and know that it is a shutdown closing your program). However,
this is a bad idea.

The other option would be to write a windows service. You can override
the OnShutdown method (along with setting the CanShutdown property to true)
and it should give you about 30 seconds to perform shutdown before it kills
your service.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ca***********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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 talk to
the UPS over a special device driver.

Through this device driver I can detect that the UPS is going to notify
Windows 2000 server to shut down. So I start doing a graceful
termination. But Windows shuts down pretty quickly and there isn't time
to close my files and gracefully shut down. Apparently, the default
console application in .NET has a default behavior of terminating
immediately -- at least that is what seems to happen.

What I need to do is to delay windows for long enough (just a few
seconds really) so that I can gracefully terminate. How do I do that
with a console project. How do I delay the time between the time that
Windows 200 Server is told to shut down and the time it actually
teminates it's applications and shuts down? I've done a lot of web
searches and .NET searches but haven't found a console solution. Maybe
all I need is a steer to the right approach.

Thanks.

Nov 17 '05 #2

P: n/a
You mean it's a bad idea because it has the potential of interfering
with shutdown and that is bad generally? This particular windows
computer's sole reason for existance is to run this one program, so
there is no user and no other important user programs running. I'm not
sure if that makes any difference to the problem, but just in case.

What about something like this in the main routine:

public static void ManageSessionEnd(
object sender,
Microsoft.Win32.SessionEndingEventArgs e
)
{
e.Cancel = false;
}

Microsoft.Win32.SessionEndingEventHandler TerminationHandler =
new Microsoft.Win32.SessionEndingEventHandler(ManageSe ssionEnd);

I don't know if this even compiles yet but it might cancel the shutdown
-- except that I don't really WANT to cancel the shutdown, only delay
it a little. This kind of approach would only work if Windows
repeatedly called this handler and shutdown when it didn't cancel the
shutdown but I'm not at all sure that is what it would do.
Documentation on this stuff seems very sparse and I've found no
examples of people trying to delay shutdown to go from.

In regards to the windows service, would I create the windows service
from my console app or would it be a totally separate program? Or are
you trying to say write the whole program as a windows service? I'm not
sure that latter option is open to me really. Or are you saying that
the windows service as a separate program is time consuming enough for
Windows to deal with that I'd buy myself a little more time?

Nov 17 '05 #3

P: n/a
I'm saying that you should write this as a service so that it is always
"on". You can then send messages into your service (via remoting) in order
to process what needs to be done (instead of invoking the program).

Using the event handler in a windows program won't work, if you cancel
the shutdown, then you won't be polled again, the shutdown is just
cancelled.

With a service, it gives you some time to clean up.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ca***********@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
You mean it's a bad idea because it has the potential of interfering
with shutdown and that is bad generally? This particular windows
computer's sole reason for existance is to run this one program, so
there is no user and no other important user programs running. I'm not
sure if that makes any difference to the problem, but just in case.

What about something like this in the main routine:

public static void ManageSessionEnd(
object sender,
Microsoft.Win32.SessionEndingEventArgs e
)
{
e.Cancel = false;
}

Microsoft.Win32.SessionEndingEventHandler TerminationHandler =
new Microsoft.Win32.SessionEndingEventHandler(ManageSe ssionEnd);

I don't know if this even compiles yet but it might cancel the shutdown
-- except that I don't really WANT to cancel the shutdown, only delay
it a little. This kind of approach would only work if Windows
repeatedly called this handler and shutdown when it didn't cancel the
shutdown but I'm not at all sure that is what it would do.
Documentation on this stuff seems very sparse and I've found no
examples of people trying to delay shutdown to go from.

In regards to the windows service, would I create the windows service
from my console app or would it be a totally separate program? Or are
you trying to say write the whole program as a windows service? I'm not
sure that latter option is open to me really. Or are you saying that
the windows service as a separate program is time consuming enough for
Windows to deal with that I'd buy myself a little more time?

Nov 17 '05 #4

P: n/a

<ca***********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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 talk to
the UPS over a special device driver.

Through this device driver I can detect that the UPS is going to notify
Windows 2000 server to shut down. So I start doing a graceful
termination. But Windows shuts down pretty quickly and there isn't time
to close my files and gracefully shut down. Apparently, the default
console application in .NET has a default behavior of terminating
immediately -- at least that is what seems to happen.

What I need to do is to delay windows for long enough (just a few
seconds really) so that I can gracefully terminate. How do I do that
with a console project. How do I delay the time between the time that
Windows 200 Server is told to shut down and the time it actually
teminates it's applications and shuts down? I've done a lot of web
searches and .NET searches but haven't found a console solution. Maybe
all I need is a steer to the right approach.

Thanks.


You can register your own Console control handler by calling Win32 API
SetConsoleCtrlHandler.
Conside following snippet as a sample.

enum CtrlType {
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
static object locker = new object();
private delegate bool EventHandler(CtrlType sig);

private static bool Handler(CtrlType sig)
{
bool handled = false;
switch (sig)
{
case CtrlType.CTRL_C_EVENT:
case CtrlType.CTRL_LOGOFF_EVENT:
case CtrlType.CTRL_SHUTDOWN_EVENT:
case CtrlType.CTRL_CLOSE_EVENT:
{
.. do whatever you need to do at shutdown time (here for all
other events too), but keep in mind that the system will kill the process
when you fail to return within 30 seconds.
}
// return true when handled, this signals the system to remove
the process
handled = true;
break;
default:
// return false when not handled
return handled;
}
return handled;
}
static EventHandler _handler;

[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler (EventHandler handler,
bool add);

static void Main()
{
// install the handler
_handler += new EventHandler(Handler);
SetConsoleCtrlHandler(_handler, true);
// and go on
....
Willy.
Nov 17 '05 #5

P: n/a
Thanks Willy that was just what I was looking for. Now I can allow the
server to wait ten seconds for my client to notice I'm shutting down
without being killed by Windows. I really appreciate your help! I
posted to the code project too. Could I cross post your reply to
http://www.codeproject.com as well in case anyone there can't figure
this out either?

You know I really wish an example like this was in the Microsoft
documentation too. I just couldn't find this information in the MSDN
library.

Nov 17 '05 #6

P: n/a

<ca***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks Willy that was just what I was looking for. Now I can allow the
server to wait ten seconds for my client to notice I'm shutting down
without being killed by Windows. I really appreciate your help! I
posted to the code project too. Could I cross post your reply to
http://www.codeproject.com as well in case anyone there can't figure
this out either?
Sure, no problem.
You know I really wish an example like this was in the Microsoft
documentation too. I just couldn't find this information in the MSDN
library.

This unmanaged SetConsoleCtrlHandler API is documented with a sample in the
platform SDK part of msdn, all you have to do is translate it into some
managed wrapper. I guess no-one cares about console applications that much,
so it's not wrapped by the System.Console class and you are a bit on your
own when looking for special things like this.
Willy.
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.