如果电量低于10%立即关闭软件a,关闭软件a后的1秒进行关机。
刚刚接触代码,想要的效果是用vs2019生成程序后就能达到上面所叙述的结果,请帮个忙。
以上是Windows平台。
修改了一下再试试。
#include <Windows.h>
#include <iostream>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
while (true) {
//获取计算机的当前电量
SYSTEM_POWER_STATUS power_staus;
int ret = GetSystemPowerStatus(&power_staus);
int battery_percent = (int)power_staus.BatteryLifePercent;
printf("%d%%\n", battery_percent);
//电量小于10%,关闭程序test.exe(使用结束进程的方法)
if (battery_percent <= 10) {
system("TASKKILL /F /IM test.exe");
//一秒之后,执行关机操作
Sleep(1000);
system("shutdown -s -f -t 0");
}
//间隔1秒循环检测
Sleep(1000);
}
}