The application runs longer on computer but if computer goes to sleep mode, there will be problem. Is there any way to prevent from sleep mode ?
Asked
Active
Viewed 3,898 times
1 Answers
3
Define a class
like this:
internal static class NativeMethods
{
// Import SetThreadExecutionState Win32 API and necessary flags
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
}
Put this to the main method: (make sure its before application.run
call)
// Set new state to prevent system sleep. (Note: still allows screen saver)
var previousExecutionState = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);

Yousha Aleayoub
- 4,532
- 4
- 53
- 64

TakeMeAsAGuest
- 957
- 6
- 11
-
1Lone link is [considered a poor answer](http://stackoverflow.com/help/deleted-answers) since it is meaningless by itself and target resource is not guaranteed to be alive in the future. Please try to include at least summary of information you are linking to. – Artemix Jul 29 '13 at 10:22