Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How to prevent a laptop from going into Stand-By

How can I do this in a C# program? I'm pretty sure it should be possible, since various media programs for example do this so the computer doesn't go into stand-by while watching a movie, etc.

So, if I for example create a plain and basic WinForm application, what do I need to do to prevent a laptop from going into Stand-By as long as this application is running?

like image 402
Svish Avatar asked Feb 03 '23 11:02

Svish


1 Answers

I think you'll have to P/Invoke. But don't be scared... it's pretty easy.

SetThreadExecutionState is your friend. It's available in the PInvoke project, also available on NuGet.

like image 97
Andrew Arnott Avatar answered Feb 06 '23 01:02

Andrew Arnott