1

Is it possible to abort computer shutdown from windows service?

TrustyCoder
  • 4,749
  • 10
  • 66
  • 119
  • 5
    Nice! Let me add another entry in my Big List of Annoying Things Programs Do. –  Apr 28 '10 at 12:46
  • Duplicate: http://stackoverflow.com/questions/589986/how-to-abort-shutdown-in-windows-xpvista-programatically – Oliver Apr 28 '10 at 12:46
  • This is not a duplicate to the question you posted. That question talks about desktop application getting a window message about a shutdown which allows for aborting. A service won't get that window message because it doesn't have a window. – Allon Guralnek Apr 28 '10 at 13:35
  • You can probably abort it in a similar way to notepad blocking windows from shutting down, when it always says "Do you wish to save changes?" and then windows has a fit trying to kill it, and if you press cancel, it's aborted! – NibblyPig Apr 28 '10 at 14:05
  • Will - I can think of several *legitimate* reasons for a windows service to do this. One would be a "watchdog" service that stops shutdowns from occuring except if they're initiated by itself, a fantastic safety net for production servers.. If someone accidentally clicks "Restart Now" in an installer prompt, having a service inhibit the shutdown would have definite value. – Rob Aug 13 '10 at 08:28

2 Answers2

0

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.

NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • 1
    `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 Apr 28 '10 at 13:29
  • 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. – NibblyPig Apr 28 '10 at 13:50
0
AbortSystemShutdown

I've added a sample to my answer at the similar question here
How cancel shutdown from a windows service C#

Community
  • 1
  • 1
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • 1
    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 Apr 28 '10 at 13:39