Search code examples
c#.netwindows-services

How do Windows services behave when entering/waking up from sleep or hibernate modes?


I have a service written in C# that does not seem to come down cleanly when the computer enters sleep mode. The user needs to manually stop and restart the service to retain functionality. I know what the issue is but I am not sure where to fix it. How do Windows services behave when going into/come out of sleep and hibernate?


Solution

  • How do services react to power events? Really, it depends.

    In your service you can handle the hibernate/suspend/etc events by handling it in your service

      protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
      { 
         //do stuff here
      }