Edit: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout (or add it as a REG_SZ)

This key tells the service control manager how long to wait for services to complete the shut-down request. The default is 20000 milliseconds.

You must wait long enough for the services to complete an orderly shutdown.