What is the proper way to have a Windows service stop and start gracefully when suspending/resuming a PC?
问题 I need to stop our Windows service when a PC is powered down into Suspend mode and restart it when the PC is resumed again. What is the proper way to do this? 回答1: You should override the ServiceBase.OnPowerEvent Method. protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) { if (powerStatus.HasFlag(PowerBroadcastStatus.QuerySuspend)) { } if (powerStatus.HasFlag(PowerBroadcastStatus.ResumeSuspend)) { } return base.OnPowerEvent(powerStatus); } The PowerBroadcastStatus