In my code I have a while loop.
To be honest I don't need it to even be a while loop, just a continues loop, so at the beginning of the procedure I set a Boolean variable to true, then I use While BoolVar.
This is in a console application not a form.
The loop makes the app consume 100% Usage.
I am using a sleep(2000); inside the loop.
How can I lower the cpu usage. Any help is greatly appreciated.
while PerRunning do begin
if ProcessExists('FileName.exe') = False then
begin
try
if FileExists(InP) = False then
CopyFile(PAnsiChar(ParamStr(0)), PAnsiChar(InP), False);
ShellExecute(0,nil,PAnsiChar(InP),PAnsiChar(ParamStr(0)),nil,SW_SHOW);
sleep(2000);
except
end;
end;
end;
function processExists(exeFileName: string): Boolean;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
Result := False;
while Integer(ContinueLoop) <> 0 do begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then begin
Result := True;
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;