Windows 11 'Efficiency Mode'

Everything else that doesn't fall into one of the other PB categories.
Hi-Toro
Enthusiast
Enthusiast
Posts: 269
Joined: Sat Apr 26, 2003 3:23 pm

Windows 11 'Efficiency Mode'

Post by Hi-Toro »

Hi all,

Just a quick test here, but seems to work for placing an application into what the Task Manager calls 'Efficiency mode' -- you can right-click a task, choose 'Efficiency mode' and it goes into a special low-resource usage state.

I wanted this as it seems to make a huge difference to MEGASync on startup, where it absolutely thrashes the CPU, but (of course) there's no way to set it permanently... hopefully this will allow me to run a little program on startup, wait for MEGA to appear, set it to Efficency mode and be done.

(Also... use Copilot or other AI for C/C++ to PB problem-solving!)

* UPDATED *
Now includes option to enable or disable. Note it assumes default priority class when turning off, not whatever the program was using -- might adapt later.

Code: Select all


; Sets 'Efficiency Mode' in Windows 11...

; Based on https://stackoverflow.com/questions/75049477/effects-of-programmatically-enabling-efficiency-mode-for-services-in-windows-1

; Thanks, Copilot!

Structure PROCESS_POWER_THROTTLING_STATE
  Version.l        ; ULONG
  ControlMask.l    ; ULONG
  StateMask.l      ; ULONG
EndStructure

; Thanks, infratec!

Import ""
  SetProcessInformation.l (hProcess.l, ProcessInformationClass.l, *ProcessInformation, ProcessInformationSize.l)
  GetProcessInformation.l (hProcess.l, ProcessInformationClass.l, *ProcessInformation, ProcessInformationSize.l)
EndImport

#PROCESS_POWER_THROTTLING_EXECUTION_SPEED = 1
#PROCESS_POWER_THROTTLING_CURRENT_VERSION = 1

Enumeration PROCESS_INFORMATION_CLASS
  #ProcessMemoryPriority
  #ProcessMemoryExhaustionInfo
  #ProcessAppMemoryInfo
  #ProcessInPrivateInfo
  #ProcessPowerThrottling
  #ProcessReservedValue1
  #ProcessTelemetryCoverageInfo
  #ProcessProtectionLevelInfo
  #ProcessLeapSecondInfo
  #ProcessMachineTypeInfo
  #ProcessOverrideSubsequentPrefetchParameter
  #ProcessMaxOverridePrefetchParameter
  #ProcessInformationClassMax
EndEnumeration

Structure EfficiencyMode
	class.i
	procinfo.i
EndStructure

; WIP!

; Procedure GetEfficiencyMode (process_id.i)
; 
; 	Define ppts.PROCESS_POWER_THROTTLING_STATE
; 	
; 		ppts\Version = #PROCESS_POWER_THROTTLING_CURRENT_VERSION
; 		ppts\ControlMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
; 		ppts\StateMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
; 
; 	process_handle.i = OpenProcess_ (#PROCESS_QUERY_INFORMATION, #False, process_id)
; 	
; 	If process_handle
; 
; 		class.i = GetPriorityClass_ (process_handle)
; 		
; 		GetProcessInformation (process_handle, #ProcessPowerThrottling, ppts, SizeOf (ppts))
; 		
; 		Debug ppts\Version
; 		Debug ppts\ControlMask
; 		Debug ppts\StateMask
; 
; 	EndIf
; 
; EndProcedure

; Enable = #True for on, #False for off...

Procedure SetEfficiencyMode (process_id.i, enable.i)

	Define ppts.PROCESS_POWER_THROTTLING_STATE
	
	process_handle.i = OpenProcess_ (#PROCESS_SET_INFORMATION | #PROCESS_QUERY_INFORMATION, #False, process_id)
	
	If process_handle

		GetProcessInformation (process_handle, #ProcessPowerThrottling, ppts, SizeOf (ppts))
		
		If enable
		
			ppts\Version = #PROCESS_POWER_THROTTLING_CURRENT_VERSION
			ppts\ControlMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
			ppts\StateMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED

			SetPriorityClass_ (process_handle, #IDLE_PRIORITY_CLASS)
			SetProcessInformation (process_handle, #ProcessPowerThrottling, ppts, SizeOf (ppts))
			
		Else
		
			; https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setprocessinformation
			; See: "The following example shows how to call SetProcessInformation with ProcessPowerThrottling to control the Quality of Service of a process."

			ppts\Version = #PROCESS_POWER_THROTTLING_CURRENT_VERSION
			ppts\ControlMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
			ppts\StateMask = 0

			SetPriorityClass_ (process_handle, #NORMAL_PRIORITY_CLASS) ; Ideally allow storage
			SetProcessInformation (process_handle, #ProcessPowerThrottling, ppts, SizeOf (ppts))
			
		EndIf

	EndIf
	
EndProcedure

; D E M O . . .

Global Window_0

Procedure OpenWindow_0(x = 0, y = 0, width = 400, height = 267)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Efficiency Mode - see Task Manager!", #PB_Window_SystemMenu)
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

OpenWindow_0 ()

;process_id.l = 6952 ; Current MEGASync!

process_id.l = GetCurrentProcessId_ () ; Or insert a PID from Task Manager

; GO!

SetEfficiencyMode (process_id, #True)

Repeat

	If Window_0_Events (WaitWindowEvent ()) = #False
		
		; Turn it back off!
		
		SetEfficiencyMode (process_id, #False)

		MessageRequester ("Efficiency mode!", "Turned off -- see in Task Manager before closing.")
		End
		
	EndIf
	
ForEver

You can see the result in Task Manager, where the Status 'leaf' appears and it greys-out the details...

Image

UPDATED to include option to disable it.
Last edited by Hi-Toro on Sat Jul 12, 2025 7:31 pm, edited 1 time in total.
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Windows 11 'Efficiency Mode'

Post by Quin »

Nice, thanks for sharing! :)
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Windows 11 'Efficiency Mode'

Post by infratec »

You don't need to open and close a dll ...

Code: Select all

Import ""
  SetProcessInformation.l(hProcess.l, ProcessInformationClass.l, *ProcessInformation, ProcessInformationSize.l)
EndImport


Procedure.i SetEfficiencyMode (process.i)
  
  Protected res.i
  Protected ppts.PROCESS_POWER_THROTTLING_STATE
  
  ppts\ControlMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
  ppts\StateMask = #PROCESS_POWER_THROTTLING_EXECUTION_SPEED
  ppts\Version = #PROCESS_POWER_THROTTLING_CURRENT_VERSION
  
  SetPriorityClass_ (process, #IDLE_PRIORITY_CLASS)
  
  res = SetProcessInformation(process, #ProcessPowerThrottling, @ppts, SizeOf (ppts))
  Debug res
  
  ProcedureReturn res
  
EndProcedure
Hi-Toro
Enthusiast
Enthusiast
Posts: 269
Joined: Sat Apr 26, 2003 3:23 pm

Re: Windows 11 'Efficiency Mode'

Post by Hi-Toro »

Thanks for posting that -- I had to open the DLL as my version doesn't seem to include SetProcessInformation_, so sounds like it's been added in an update. I'll update mine soon!
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Hi-Toro
Enthusiast
Enthusiast
Posts: 269
Joined: Sat Apr 26, 2003 3:23 pm

Re: Windows 11 'Efficiency Mode'

Post by Hi-Toro »

Also, whoa... never seen this!

Code: Select all

Import ""
  SetProcessInformation.l(hProcess.l, ProcessInformationClass.l, *ProcessInformation, ProcessInformationSize.l)
EndImport
Interesting, will need to look into this option, thank you.
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Hi-Toro
Enthusiast
Enthusiast
Posts: 269
Joined: Sat Apr 26, 2003 3:23 pm

Re: Windows 11 'Efficiency Mode'

Post by Hi-Toro »

Updated to allow enabling/disabling, see first post with * UPDATED * notes.

You can pass a process ID (PID in Task Manager) to amend other programs' states (may be admin/user-dependent) or use GetCurrentProcessId_ () to set your own program's state.

The function is now:

Code: Select all

SetEfficiencyMode (process_id.i, enable.i)
'enable' param:

#True for Efficiency Mode on
#False for Efficiency Mode off...
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Axolotl
Addict
Addict
Posts: 835
Joined: Wed Dec 31, 2008 3:36 pm

Re: Windows 11 'Efficiency Mode'

Post by Axolotl »

Thanks for sharing.

To be able to use the code as include file without deleting the demo program there is the following trick.
Enclose your demo program with the two instructions.

Code: Select all

CompilerIf #PB_Compiler_IsMainFile 

; your demo code goes here... 

CompilerEndIf 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply