Skip to content
This repository was archived by the owner on Jul 24, 2025. It is now read-only.

Commit 206cf2d

Browse files
Fix #1553
1 parent f7df596 commit 206cf2d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

LenovoLegionToolkit.Lib/NativeMethods.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,15 @@ EFFECTIVE_POWER_MODE_V2
9999

100100
NOTIFYICONDATAW
101101

102-
WLAN_CONNECTION_NOTIFICATION_DATA
102+
WLAN_CONNECTION_NOTIFICATION_DATA
103+
104+
PROCESS_POWER_THROTTLING_STATE
105+
PROCESS_POWER_THROTTLING_CURRENT_VERSION
106+
PROCESS_POWER_THROTTLING_EXECUTION_SPEED
107+
108+
GetCurrentProcess
109+
SetProcessInformation
110+
SetPriorityClass
103111

104112
GetSystemInfo
105113
CallNtPowerInformation

LenovoLegionToolkit.WPF/Windows/MainWindow.xaml.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6+
using System.Runtime.InteropServices;
67
using System.Threading.Tasks;
78
using System.Windows;
89
using System.Windows.Input;
@@ -19,12 +20,16 @@
1920
using LenovoLegionToolkit.WPF.Utils;
2021
using LenovoLegionToolkit.WPF.Windows.Utils;
2122
using Microsoft.Xaml.Behaviors.Core;
23+
using Windows.Win32;
24+
using Windows.Win32.System.Threading;
2225
using Wpf.Ui.Controls;
2326
#if !DEBUG
2427
using System.Reflection;
2528
using LenovoLegionToolkit.Lib.Extensions;
2629
#endif
2730

31+
#pragma warning disable CA1416
32+
2833
namespace LenovoLegionToolkit.WPF.Windows;
2934

3035
public partial class MainWindow
@@ -146,9 +151,11 @@ private void MainWindow_StateChanged(object? sender, EventArgs e)
146151
switch (WindowState)
147152
{
148153
case WindowState.Minimized:
154+
SetEfficiencyMode(true);
149155
SendToTray();
150156
break;
151157
case WindowState.Normal:
158+
SetEfficiencyMode(false);
152159
BringToForeground();
153160
break;
154161
}
@@ -327,7 +334,41 @@ public void SendToTray()
327334
if (!_applicationSettings.Store.MinimizeToTray)
328335
return;
329336

337+
SetEfficiencyMode(true);
330338
Hide();
331339
ShowInTaskbar = true;
332340
}
341+
342+
private static unsafe void SetEfficiencyMode(bool enabled)
343+
{
344+
var ptr = IntPtr.Zero;
345+
346+
try
347+
{
348+
var priorityClass = enabled
349+
? PROCESS_CREATION_FLAGS.IDLE_PRIORITY_CLASS
350+
: PROCESS_CREATION_FLAGS.NORMAL_PRIORITY_CLASS;
351+
PInvoke.SetPriorityClass(PInvoke.GetCurrentProcess(), priorityClass);
352+
353+
var state = new PROCESS_POWER_THROTTLING_STATE
354+
{
355+
Version = PInvoke.PROCESS_POWER_THROTTLING_CURRENT_VERSION,
356+
ControlMask = PInvoke.PROCESS_POWER_THROTTLING_EXECUTION_SPEED,
357+
StateMask = enabled ? PInvoke.PROCESS_POWER_THROTTLING_EXECUTION_SPEED : 0,
358+
};
359+
360+
var size = Marshal.SizeOf<PROCESS_POWER_THROTTLING_STATE>();
361+
ptr = Marshal.AllocHGlobal(size);
362+
Marshal.StructureToPtr(state, ptr, false);
363+
364+
PInvoke.SetProcessInformation(PInvoke.GetCurrentProcess(),
365+
PROCESS_INFORMATION_CLASS.ProcessPowerThrottling,
366+
ptr.ToPointer(),
367+
(uint)size);
368+
}
369+
finally
370+
{
371+
Marshal.FreeHGlobal(ptr);
372+
}
373+
}
333374
}

0 commit comments

Comments
 (0)