简体   繁体   中英

Prevent Windows desktop application from interrupting sleep

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM