在我的代碼中我有一個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