I have noticed that when I run Windows Media Player on my laptop, that the screen saver will not start and more importantly my computer will not go into sleep or hibernate mode. However, when running iTunes or other applications, unless you move your mouse or hit a key on your keyboard your computer will eventually go into sleep or hibernate mode. I want to write a little application that will prevent my computer from going into sleep or hibernate mode. Does anyone have any ideas on how this could be done?


For application - you may want to look at win32 function SetThreadExecutionState

Anyway, you could set different power scheme in windows control panel to let computer stay awake all the time.

  • I can't get this method to work on Windows 10. It works fine on Windows 8/7. Do you have any thoughts on how to get this working on Windows 10? – Dan Gifford Nov 9 '15 at 14:47

You've got several events to handle to acheive this. First up, process the WM_SYSCOMMAND message, specifically the SC_SCREENSAVE and SC_MONITORPOWER options. Secondly, handle the WM_POWERBROADCAST message and return BROADCAST_QUERY_DENY.

However, MS decided to allow the BROADCAST_QUERY_DENY to be ignored by the system so it'll only work where it's allowed to work, usually a user setting. Too many applications were preventing power-down that the feature was effectively redundant.



Have a look at insomnia, which if I understand correctly is exactly the application you want. Note that both binary and source code are available, so if you prefer to roll your own, you can use that as a starting point.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy