views:

124

answers:

2

Hi, Is it possible to abort computer shutdown from windows service?

A: 

Yes. You can call shutdown /a using Process.Start

static void Main(string[] args)
{
    Process p = new Process();
    p.StartInfo.FileName = "shutdown";
    p.StartInfo.Arguments = "/r";
    p.Start();
    Thread.Sleep(5000);
    p.StartInfo.Arguments = "/a";
    p.Start();

}

The above code tells the computer to shutdown and restart, waits 5 seconds, then aborts it.

SLC
`shutdown /a` only works for shutdowns that have a time-out period. If a user clicks Start >> Shut Down, `shutdown /a` won't abort it.
Allon Guralnek
Will anything abort it? And even if it does, chances are half your processes have ended anyway, it's probably not a good idea to abort it at that stage.
SLC
A: 
AbortSystemShutdown

I've added a sample to my answer at the similar question here
http://stackoverflow.com/questions/2720125/how-cancel-shutdown-from-a-windows-service-c/2720191#2720191

ho1
This will not work for the same reason SLC's answer won't work - AbortSystemShutdown will only succeed when called "During the shutdown time-out period" [MSDN quote]. When a user initiates a shut down, there is no time-out period.
Allon Guralnek
I'd missed that, thanks.
ho1