Jump to content
Sign in to follow this  
Pamen

Efficiency Mode for own app (FMX, Delphi12, Win11)

Recommended Posts

Hi

does anyone know how to force Windows 11 "Efficiency Mode" for main process of the application at start?

Lowering process priority does not do it as Efficiency mode requires additional flags set. 

An Windows API in Delphi lacks the additional process flags...

Share this post


Link to post

You can use WINMD or better this https://www.winsoft.sk/win32api.htm to get the update informations that you need. (EDIT: the link is taken from some topic of this forum)

 

By now, I know little bit, seems that Microsoft is still working on this and the things will change (like an extensions of API function).

 

For example the new definition of PROCESS_INFORMATION_CLASS is:

 

 PROCESS_INFORMATION_CLASS = (
    ProcessMemoryPriority,
    ProcessMemoryExhaustionInfo,
    ProcessAppMemoryInfo,
    ProcessInPrivateInfo,
    ProcessPowerThrottling,
    ProcessReservedValue1,
    ProcessTelemetryCoverageInfo,
    ProcessProtectionLevelInfo,
    ProcessLeapSecondInfo,
    ProcessMachineTypeInfo,
    ProcessInformationClassMax
  );

A little bit different from old one.

Edited by DelphiUdIT
  • Thanks 1

Share this post


Link to post

You can try this, derivated from Learn Microsoft - EcoQS and updated for Delphi (sorry for errors):

 

//I use _my to discriminate from bundle definitions

uses WinApi.Windows;    //Add this to uses unit

type
  ///<summary>Documentation: https://docs.microsoft.com/windows/win32/api/processthreadsapi/ne-processthreadsapi-process_information_class</summary>
  PROCESS_INFORMATION_CLASS_my = (
    ProcessMemoryPriority,
    ProcessMemoryExhaustionInfo,
    ProcessAppMemoryInfo,
    ProcessInPrivateInfo,
    ProcessPowerThrottling,
    ProcessReservedValue1,
    ProcessTelemetryCoverageInfo,
    ProcessProtectionLevelInfo,
    ProcessLeapSecondInfo,
    ProcessMachineTypeInfo,
    ProcessInformationClassMax
  );

type
  _PROCESS_POWER_THROTTLING_STATE = record
    Version: ULONG;
    ControlMask: ULONG;
    StateMask: ULONG;
  end;
  PROCESS_POWER_THROTTLING_STATE = _PROCESS_POWER_THROTTLING_STATE;
  PPROCESS_POWER_THROTTLING_STATE = ^PROCESS_POWER_THROTTLING_STATE;

const
  PROCESS_POWER_THROTTLING_CURRENT_VERSION = 1;
  PROCESS_POWER_THROTTLING_EXECUTION_SPEED = $1;

//Redefine API
function SetProcessInformation_my(hProcess: THandle; ProcessInformationClass: PROCESS_INFORMATION_CLASS_my;
  ProcessInformation: Pointer; ProcessInformationSize: DWORD): ByteBool; stdcall;
function SetProcessInformation_my; external kernel32 name 'SetProcessInformation' delayed;

implementation

{$R *.fmx}

function SetEfficiencyMode(PID: DWORD): Boolean;
var
  hProcess: THandle;
  PowerState: PROCESS_POWER_THROTTLING_STATE;
begin
  Result := false;
  hProcess := OpenProcess(PROCESS_SET_INFORMATION, False, PID);
  if hProcess <> 0 then
    begin
      ZeroMemory(@PowerState, SizeOf(PowerState));
      PowerState.Version := PROCESS_POWER_THROTTLING_CURRENT_VERSION;
      PowerState.ControlMask := PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
      PowerState.StateMask := PROCESS_POWER_THROTTLING_EXECUTION_SPEED;

      Result := SetProcessInformation_my(hProcess, Process_Information_Class_my(ProcessPowerThrottling), @PowerState, SizeOf(PowerState));

      CloseHandle(hProcess);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var Success: boolean;
begin
  Success := SetEfficiencyMode(GetCurrentProcessId);
  if Success  then
    ShowMessage('Efficiency mode activated successfully!')
  else
    ShowMessage('Error activating efficiency mode.');
end;

 

  • Like 1
  • Confused 1

Share this post


Link to post

Thank you very much both!

The second answer seems AI generated though - generally a good idea, cannot work.

Wish you a good Week.

Edited by Pamen

Share this post


Link to post
45 minutes ago, Pamen said:

Thank you very much both!

The second answer seems AI generated though - generally a good idea, cannot work.

Wish you a good Week.

Why ?

https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setprocessinformation

https://learn.microsoft.com/en-us/answers/questions/4052040/efficiency-mode-causing-performance-issue

 

This come from FireFox, and it's the UNIQUE in my 260 processes and 5000 Thread that go in a efficiency mode alone .... No other process known go in that mode, except if you force it with Task Manager.

 

It is at "firefox-release\hal\windows\WindowsProcessPriority.cpp", you can translate in Delphi.

 

image.thumb.png.41fc54ebffadec5cff3707e67055417a.png

 

 

Edited by DelphiUdIT
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×