I have a program that reads the memory of a computer process continuously (with while (true)
), but this ends up requiring a lot of CPU, arriving at 20% used, my question is, how to decrease CPU usage without lose performance in the program?
I tried Thread.Sleep(10)
, it worked, CPU usage dropped to 1%, but even though it was only 10 thousandths, program performance dropped dramatically.
while (Vars.GlowActive) //Essa variavel sempre vai ser true
if (Vars.GlowAlways || Vars.GetKeyState(Vars.GlowButton) == 0)
{
for (int num = 1; num < 24; num++)
{
int entity = mem.Read<int>(Vars.bClient + Vars.EntList + (num * 0x10));
ehealth = mem.Read<int>(entity + Vars.Health);
int Glow = mem.Read<int>(entity + Vars.GlowIndex);
int EntTeam = mem.Read<int>(entity + Vars.Team);
if (EntTeam == Vars.MyTeam)
{
if (Vars.glowteamenabled)
{
if (Vars.TeamRainbow)
TeamColor();
color[0] = Vars.glow_team_r / 255;
color[1] = Vars.glow_team_g / 255;
color[2] = Vars.glow_team_b / 255;
DrawGlow(Glow, color);
}
}
else
{
if (Vars.glowenemyenabled)
{
if (Vars.EnemyRainbow)
EnemyColor();
else if (Vars.glowhealth)
{
Vars.glow_enemy_r = 255 - 2.55f * ehealth;
Vars.glow_enemy_g = 2.55f * ehealth;
Vars.glow_enemy_b = 0;
}
color[0] = Vars.glow_enemy_r / 255;
color[1] = Vars.glow_enemy_g / 255;
color[2] = Vars.glow_enemy_b / 255;
DrawGlow(Glow, color);
}
}
}
}
else if (one && Vars.BeepEnable)
{
Console.Beep(3000, 200);
one = !one;
}
}
Summary:
mem.Read
reads the memory of a process.
DrawGlow()
writes in memory.