4
I have a program that reads the memory of a computer process continuously (with the while (true)
), but it ends up requiring a lot of CPU, arriving at 20% used, my question is, how to decrease CPU usage without losing performance in the program?
I tried the Thread.Sleep(10)
, has worked, CPU usage has dropped to 1%, but even though it is only 10 thousandths, program performance has 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;
}
}
Sumario:
mem.Read
reads the memory of a process.
DrawGlow()
writes in memory.
Instead of while couldn’t you use an event routine? Or even a timer: https://msdn.microsoft.com/pt-br/library/system.timers.timer.elapsed(v=vs.110). aspx do not understand much of C#, anyway I believe it was not to fall performance when using
Thread.Sleep
, may be some "conflict".– Guilherme Nascimento
How does an event routine work? Never heard of it...
– Francisco
Maybe I didn’t choose the best words, for example using Ontimedevent to run in parallel. I still believe that it was not to have fallen the performance just because of a Sleep. That
Thread
may be running multiple times? Or something? Maybe it is possible to solve in your code and the problem is not even Sleep.– Guilherme Nascimento
No, it runs only once, but it is inside a while true. I will put the code in the question.
– Francisco
You settled with the
System.Timers.Timer
? I almost got xD right– Guilherme Nascimento
@Yes, it was close. = D
– Francisco