Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Windows awake when a WPF application is running

Is there any way (short of manually doing so in the Windows settings each time I run it) to make a running WPF application prevent Windows from going to sleep/hibernating/etc. The application is just running a continuous loop every 10 seconds in a BackgroundWorker. I noticed when my machine auto-slept, it stopped my BackgroundWorker, and I'd like to keep it running indefinitely.

If its not possible purely in WPF, are there other ways?

like image 529
Wilson Avatar asked Dec 31 '25 17:12

Wilson


1 Answers

You can use the SetThreadExecutionState API function. More information is given here

However, don't do that if you don't really have to, and certainly don't do that before getting the user's explicit consent. Your users will be pissed off when they realize their laptops die while your program is running. So warn them properly so they're not pissed off.

like image 197
zmbq Avatar answered Jan 04 '26 20:01

zmbq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!