This is one of the features of the Windows service.
Shutdown
Shutdown occurs automatically when you turn off the PC. No need to do anything. To perform any cleanup, you need to override methods like ServiceBase , such as OnPowerEvent , a sample
public class WinService : ServiceBase { protected override void OnStart(string[] args) { ... } protected override void OnStop() { ... } protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) { ... } }
Start
To start the service automatically, you need to install it ServiceStartMode.Automatic , as here
[RunInstaller(true)] public class WindowsServiceInstaller : Installer { private readonly ServiceProcessInstaller _process; private readonly ServiceInstaller _service; public WindowsServiceInstaller() { _process = new ServiceProcessInstaller { Account = ServiceAccount.LocalSystem }; _service = new ServiceInstaller { ServiceName = "FOO", StartType = ServiceStartMode.Automatic,
source share