Search code examples
windowssystem-shutdown

Shut down Windows programmatically and wait for termination of resident processes


Background

  • I am writing a program to shut down Windows at the specified time. I am using shutdown.exe to implement this function.
  • In my computer, there is a resident process to record system activity and send the record to a server when shutting down Windows. When you shut down Windows from the start menu (RuntimeBroker.exe), Windows waits for the resident process to finish sending the record (according to HKCU\Control Panel\Desktop\HungAppTimeout).

Problem

When I shut down Windows with shutdown.exe, it doesn't refer to HKCU\Control Panel\Desktop\HungAppTimeout and kills the resident process forcibly before it finishes sending the record.

Question

Is there any way (like a PowerShell cmdlet or a command included in Windows) to shut down Windows programmatically and wait for the termination of the resident processes gracefully before the shutdown?


Solution

  • Based on @An-dir's advice, I have tested some ways to shutdown Windows:

    # How to shutdown What is executed Wait for HungAppTimeout
    1 shutdown from Start Menu RuntimeBroker.exe Yes
    2 run shutdown.exe shutdown.exe No
    3 run SlideToShutDown.exe SlideToShutDown.exe Yes
    4 run Stop-Computer cmdlet WmiPrvSE.exe Yes
    5 shut down from the lock screen winlogon.exe No
    6 Alt+F4 on Desktop Explorer.exe Yes

    For my purpose, Stop-Computer and SlideToShutDown.exe look suitable.