As the owner of an old laptop with a half-dead battery, I am deeply concerned about the extension of its use without power. Knowing that in Windows there is a powerful tool for saving energy, I started to make it as convenient as possible to use it. I want to share the results with you.
I'm not very aware of how things are now in terms of battery life on modern laptops. I think that few people can make a MacBook competition, but since on duty I use Windows, my situation is quite bad. Although feedback on the unsatisfactory operating time on Windows is nowhere particularly audible. It feels like modern devices provide an acceptable working time or in all usage scenarios somewhere near an outlet.
Nevertheless, I, as a student and owner, are by no means an advanced laptop seriously worried about saving energy. In Windows 7 and higher, there is a power management subsystem in which there are 3 pre-installed schemes:
The latter is installed by default. If you are not satisfied with the average position and you want maximum performance when working from the mains and maximum life when working on battery, then it is logical that when power is turned off, an energy saving circuit should be installed, and when connected back, high performance isn’t it? And no. There is no direct way to switch automatically. You can create your own power scheme and customize the use of resources for two scenarios within it. But does anyone want to spend 20 minutes in this window?
In addition, in Windows 10 there was a great button to save energy, but as far as I know, it only applies to blocking the background activities of UWP applications, which is not very interesting.
The first time I opened the power settings in this way every time the power cord was unplugged and when it was plugged back. It looks quite harmless until it becomes a routine activity that you do dozens of times a day.
Then the mind leapt and invented the use of bat-files to run simple commands in the console, including certain power management schemes.
It took me longer, but the awareness of the wildness of what I do gradually came in and in one sleepless night the simplest WinForms application in C # was written, which listens to a system event and when the power mode changes, it activates a specific power scheme.
In the implementation, even timers were avoided, which means that the application should have practically no effect on system performance while constantly running.
We hang the handler on the system event:
SystemEvents.PowerModeChanged += OnPowerModeChanged;
And we write the handler itself:
private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e) { if (e.Mode == PowerModes.StatusChange) { switch (SystemInformation.PowerStatus.PowerLineStatus) { case PowerLineStatus.Online: DisablePowerSaving(); break; case PowerLineStatus.Offline: EnablePowerSaving(); break; } } }
Everything is trite and simple. Switching modes occurs by running the powercfg
utility on the command line, passing the GUID of the power scheme we need:
private void EnablePowerSaving() { RunCmdCommand("powercfg /setactive a1841308-3541-4fab-bc81-f71556f20b4a"); }
All code is launched from a hidden form, mode change messages are localized in Russian and English. The program is put into autoload by copying to the Startup folder, there is even a special bat in the repository.
→ By the way, here is the repository .
The application is tested on Windows 10, should work correctly on Windows 7 and above. On Windows XP and Vista, problems will arise with the paths to the Startup folder. I hope for your feedback, if there is a demand, support will be surely added.
Thanks for attention. This was my first post, I will be sincerely happy about the criticism of the code and the design of the article.
Source: https://habr.com/ru/post/321628/
All Articles