Prevent the System from Entering Sleep or Turning off the Display

106 30
Submitted by Jens Borrisholt
Windows detects activities such as keyboard or mouse input - after a "longer" idle time the system might go to sleep mode and/or power off the display device.

If you have a critical application running and you want to prevent the system from entering sleep or turning off the display while the application is running you need to call a Windows API function named SetThreadExecutionState.

TSystemCritical - wrapper for SetThreadExecutionState

The SystemCriticalU unit holds the wrapper for the SetThreadExecutionState API.
An instance of the TSystemCritical is created when the unit is initialized and destroyed in the finalization section. Therefore, to use the the SystemCritical global object you simply set its only property, "IsCritical" to either true or false.

When, during the lifecycle of you application, you need to enter into a critiacal execution section, you can set SystemCritical.IsCritical = true;.

unit SystemCriticalU;

interface
uses
  Windows;

type
  TSystemCritical = class
  private
    FIsCritical: Boolean;
    procedure SetIsCritical(const Value: Boolean) ;
  protected
    procedure UpdateCritical(Value: Boolean) ; virtual;
  public
    constructor Create;
    property IsCritical: Boolean read FIsCritical write SetIsCritical;
  end;

var
  SystemCritical: TSystemCritical;

implementation

{ TSystemCritical }
//REF : http://msdn.microsoft.com/en-us/library/aa373208.aspx
type
  EXECUTION_STATE = DWORD;
const
  ES_SYSTEM_REQUIRED = $00000001;
  ES_DISPLAY_REQUIRED = $00000002;
  ES_USER_PRESENT = $00000004;
  ES_AWAYMODE_REQUIRED = $00000040;
  ES_CONTINUOUS = $80000000;
  KernelDLL = 'kernel32.dll';

(*
    SetThreadExecutionState Function

   Enables an application to inform the system that it is in use,
   thereby preventing the system from entering sleep or turning off the display while the application is running.
  *)


procedure SetThreadExecutionState(ESFlags: EXECUTION_STATE) ; stdcall; externalkernel32 name 'SetThreadExecutionState';

constructor TSystemCritical.Create;
begin
  inherited;
  FIsCritical := False;
end;

procedure TSystemCritical.SetIsCritical(const Value: Boolean) ;
begin
  if FIsCritical = Value then Exit;

  FIsCritical := Value;
  UpdateCritical(FIsCritical);
end;

procedure TSystemCritical.UpdateCritical(Value: Boolean) ;
begin
  if Value then//Prevent the sleep idle time-out and Power off.
    SetThreadExecutionState(ES_SYSTEM_REQUIRED or ES_CONTINUOUS)
  else//Clear EXECUTION_STATE flags to disable away mode and allow the system to idle to sleep normally.
    SetThreadExecutionState(ES_CONTINUOUS) ;
end;

initialization
  SystemCritical := TSystemCritical.Create;
finalization
  SystemCritical.IsCritical := False;
  SystemCritical.Free;
end.


Here's one usage example:
SystemCritical.IsCritical = true;
try
//do critical operation here
//without going in to sleep orturning off the display

finally
  SystemCritical.IsCritical = false;
end;

Delphi tips navigator:
» FirstDelimiter Delphi Function - Returns Index of the First Occurence in a String of the Characters Specified
« Is your Delphi Application Running under Terminal Services as a Remote Session?

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.