Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect system standby/sleep/hibernate from .NET Windows Service?

In a Windows Service, I need to detect when the computer goes into sleep and when it wakes up, and run some code on these events. This needs to be done for sleep, standby and hibernate power modes.

How do I do that ?

like image 285
driis Avatar asked Jan 02 '10 17:01

driis


2 Answers

Override the ServiceBase.OnPowerEvent() method.

like image 98
Hans Passant Avatar answered Sep 30 '22 15:09

Hans Passant


I'm not sure because I have never did it myself but recently I came accross OnSessionChange method. I can't promise it allows to catch sleep event, but may be studying it's documentation in MSDN or elswere can help you.

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.canhandlesessionchangeevent.aspx

like image 40
Sergej Andrejev Avatar answered Sep 30 '22 16:09

Sergej Andrejev