I have a Windows desktop program (Tweetz) that interrupts/prevents sleep mode when running. The nature of the program is to monitor twitter and update the UI as tweets arrive. I suspect the network activity and/or updating the UI is preventing/interrupting sleep mode.
Is there a System API that can tell Windows that an application should not prevent sleeping?
2
Answers
I would suggest the following approach. Code the Twitter monitor in a windows service and buffer any message that you need to display. For these messages, check if the system is in sleep mode or if the desktop is locked (according to your applications specifications). Set up some communication mechanism with your desktop application and transfer messages when needed/possible. The service won’t prevent your system from going to sleep, you can monitor session state changes with SetServiceStatus() using SERVICE_ACCEPT_SESSIONCHANGE. You could also use the OnSessionChange event.
Perhaps Windows successfully sleeps but your app immediately wakes it up by sending network traffic. If so, you could handle
WM_POWERBROADCAST
messages, especially PBT_APMQUERYSUSPEND and PBT_APMRESUMEAUTOMATIC. Stop network traffic on suspend, and restart it on Resume.