views:

721

answers:

1

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?

+4  A: 

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

SetThreadExecutionState is your friend. And PInvoke.net will tell you how to call this function from C#.

Andrew Arnott
Looks perfect! Can't test it until tonight or tomorrow though, but it looks right :)
Svish
Do I have to set the state back when my application exits? Or, since it is a per thread/application thing, will it go back to normal automatically?
Svish
Unless the documentation states you need to go back manually, I'd say that you probably don't have to set it back if you mean for it to last the life of your app.
Andrew Arnott