在我的代码中我有一个while循环。德尔福 - while循环消费CPU
说实话,我不需要它甚至是一个while循环,只是一个继续循环,所以在过程开始时,我将一个布尔变量设置为true,然后使用While BoolVar。
这是在控制台应用程序中不是表单。
循环使应用程序消耗100%的使用量。
我正在使用睡眠(2000);在循环内。
如何降低CPU使用率。任何帮助是极大的赞赏。
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;
可能ProcessExists()返回True,所以Sleep()永远不会执行 – 2012-07-21 07:29:04
函数processExists(exeFileName:string):Boolean; var ContinueLoop:BOOL; FSnapshotHandle:THandle; FProcessEntry32:TProcessEntry32; 开始 FSnapshotHandle:= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); FProcessEntry32.dwSize:= SizeOf(FProcessEntry32); ContinueLoop:= Process32First(FSnapshotHandle,FProcessEntry32); 结果:=假;如果((UpperCase(ExtractFileName(FProcessEntry32.szExeFile))= UpperCase(ExeFileName))或(UpperCase(FProcessEntry32),则Integer(ContinueLoop)<> 0 do begin 。szExeFile)= – user1512695 2012-07-21 07:32:17
UpperCase(ExeFileName)))然后 开始 结果:=真; 结束; ContinueLoop:= Process32Next(FSnapshotHandle,FProcessEntry32); 结束; CloseHandle(FSnapshotHandle); 结束; – user1512695 2012-07-21 07:33:42