Sunday, March 04, 2012

Handling pre-shutdown notification in C# Windows service


The need for pre-shutdown notification


Enabling pre-shutdown notification



  FieldInfo acceptedCommandsFieldInfo =
  if (acceptedCommandsFieldInfo == null)
    throw ApplicationException("acceptedCommands field not found");
  int value = (int)acceptedCommandsFieldInfo.GetValue(this);
  acceptedCommandsFieldInfo.SetValue(this, value | SERVICE_ACCEPT_PRESHUTDOWN);

Handling pre-shutdown notification
protected override void OnCustomCommand(int command)
      // do the clean-up here


gaaah said...

Hi, how did you know that "acceptedCommands" was a field that was available? Or do these fields only show up at runtime? I couldn't find them with the Object broswer. Thanks, you saved me from attempting a bunch of pinvoke madness. --tom

Siva Chandran P said...

It may not show up in the ObjectBrowser but it shows up in Reflector as private field.

Anonymous said...

Is the code provided GPL, i.e. may I use it for a GPL project?


Siva Chandran P said...

Yes, you can use it in your project.

Unknown said...


When using "shutdown -s -f -t 3600".

How can i avoid the "system will shutdown in 10 minutes" notification.??

Thank you!
Diego M.

Siva Chandran P said...

I don't think we can control that, but I am not sure.

Kimberly Cox said...

Thank you for such advice, I just wanted to turn off these notices, because they terribly annoyed me. Also, if you suddenly have problems with api-ms-win-crt-runtime-l1-1-0.dll missing I can recommend the site that I use. There are instructions and articles, as well as files that are needed for installation. It always helping to me with my windows.