+ TProcess.WaitForExit implementation with timeout

git-svn-id: trunk@32991 -
This commit is contained in:
florian 2016-01-24 09:38:01 +00:00
parent d2a7f17d8c
commit bdbdee6acf
6 changed files with 62 additions and 7 deletions
packages/fcl-process/src

View File

@ -143,6 +143,11 @@ begin
Result:=True;
end;
Function TProcess.WaitOnExit(Timeout : DWord) : Boolean;
begin
Result:=True;
end;
Function TProcess.Suspend : Longint;
begin
Result:=0;

View File

@ -126,6 +126,11 @@ begin
Result:=True;
end;
Function TProcess.WaitOnExit(Timeout : DWord) : Boolean;
begin
Result:=True;
end;
Function TProcess.Suspend : Longint;
begin
Result:=0;

View File

@ -121,6 +121,7 @@ Type
Function Suspend : Integer; virtual;
Function Terminate (AExitCode : Integer): Boolean; virtual;
Function WaitOnExit : Boolean;
Function WaitOnExit(Timeout : DWord) : Boolean;
Property WindowRect : Trect Read GetWindowRect Write SetWindowRect;
Property Handle : THandle Read FProcessHandle;
Property ProcessHandle : THandle Read FProcessHandle;

View File

@ -459,6 +459,27 @@ begin
FRunning:=False;
end;
Function TProcess.WaitOnExit(Timeout : DWord) : Boolean;
var
res: cint;
begin
Result:=false;
res:=fpWaitPid(Handle,pcint(@FExitCode),WNOHANG);
while (res=-1) and (fpgeterrno=ESysEINTR) do
begin
if Timeout=0 then
Exit;
Sleep(1);
dec(Timeout);
res:=fpWaitPid(Handle,pcint(@FExitCode),WNOHANG);
end;
result:=res=Handle;
If Not Result then
FexitCode:=cardinal(-1) // was 0, better testable for abnormal exit.
else
FRunning:=False;
end;
Function TProcess.Suspend : Longint;
begin

View File

@ -36,10 +36,8 @@ begin
end;
Function TProcess.PeekExitStatus : Boolean;
begin
GetExitCodeProcess(ProcessHandle,FExitCode);
Result:=(FExitCode<>Still_Active);
Result:=GetExitCodeProcess(ProcessHandle,FExitCode) and (FExitCode<>Still_Active);
end;
Function GetStartupFlags (P : TProcess): Cardinal;
@ -137,6 +135,7 @@ begin
FillChar(SI,SizeOf(SI),0);
With SI do
begin
cb:=SizeOf(SI);
dwFlags:=GetStartupFlags(P);
if P.FShowWindow<>swoNone then
dwFlags:=dwFlags or Startf_UseShowWindow
@ -307,10 +306,8 @@ Var
end;
Function TProcess.WaitOnExit : Boolean;
Var
R : DWord;
begin
R:=WaitForSingleObject (FProcessHandle,Infinite);
Result:=(R<>Wait_Failed);
@ -319,6 +316,19 @@ begin
FRunning:=False;
end;
Function TProcess.WaitOnExit(Timeout : DWord) : Boolean;
Var
R : DWord;
begin
R:=WaitForSingleObject (FProcessHandle,Timeout);
Result:=R=0;
If Result then
begin
GetExitStatus;
FRunning:=False;
end;
end;
Function TProcess.Suspend : Longint;
begin

View File

@ -243,10 +243,8 @@ begin
end;
Function TProcess.WaitOnExit : Boolean;
Var
R : DWord;
begin
R:=WaitForSingleObject (FProcessHandle,Infinite);
Result:=(R<>Wait_Failed);
@ -255,6 +253,21 @@ begin
FRunning:=False;
end;
Function TProcess.WaitOnExit(Timeout : DWord) : Boolean;
Var
R : DWord;
begin
R:=WaitForSingleObject (FProcessHandle,Timeout);
Result:=R=0;
If Result then
begin
GetExitStatus;
FRunning:=False;
end;
end;
Function TProcess.Suspend : Longint;
begin