Browse Source

+ TProcess.WaitForExit implementation with timeout

git-svn-id: trunk@32991 -
florian 9 years ago
parent
commit
bdbdee6acf

+ 5 - 0
packages/fcl-process/src/amicommon/process.inc

@@ -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;

+ 5 - 0
packages/fcl-process/src/dummy/process.inc

@@ -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;

+ 1 - 0
packages/fcl-process/src/process.pp

@@ -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;

+ 21 - 0
packages/fcl-process/src/unix/process.inc

@@ -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

+ 15 - 5
packages/fcl-process/src/win/process.inc

@@ -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

+ 15 - 2
packages/fcl-process/src/wince/process.inc

@@ -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