Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Problem mit WaitForSingleObject (https://www.delphipraxis.net/26175-problem-mit-waitforsingleobject.html)

Nightshade 19. Jul 2004 17:42


Problem mit WaitForSingleObject
 
Ich hab da ein kleines Problem :

Wenn ich die untenstehende Procedure (Hab ich hier aus dem Forum) aufrufe bleibt er in :

Delphi-Quellcode:
     repeat
        WaitHandle := WaitForSingleObject( ProcessInfo.hProcess,30);
        Application.ProcessMessages;
      until (WaitHandle <> WAIT_TIMEOUT) or application.terminated;
dieser Schleife hängen.

In test.php setht nur ein simples
Code:
<?phpinfo();?>
Wenn ich nur einen PHP-Syntaxcheck mache (mit dieser Prozedur) funtioniert alles, aber mit dem Ausführen des Ganzen harperts irgendwie.

Der Path existiert und die test.php findet er auch (wenn ich den PHP.EXE Prozess kille sehe ich einen Anfang der PHPINFO() )

Ahja, der Aufruf auf der Kommandozeile dauert ca. 0,5 Sec. (im Fenster geteset).

Hat da jemand ne Idee ?

Aufruf :
Delphi-Quellcode:
    phpexe := 'c:\php.php.exe';
    pathname := 'd:\Apache Group\Apache\htdocs\test.php';
    tmp := TStringList.Create;
    x := phpexe + ' -f "' + pathname + '"';
    CaptureDosCmd(x,tmp);
    [...]
Procedure :
Delphi-Quellcode:
procedure TEditForm.CaptureDosCmd(command:String; var OutputMemo:TStringList);
const
  CaptureBufferSize = 64000;
var
  SecAttrib          : TSecurityAttributes;
  ReadPipe,writePipe : THandle;
  Startup            : TStartUpInfo;
  ProcessInfo        : TProcessInformation;
  CaptureBuffer      : Pchar;
  BytesRead          : DWord;
  WaitHandle         : DWord;
begin
  OutPutMemo.clear;
//  OutputMemo.add('# Starte "'+command+'"');
  With SecAttrib do begin
    nlength             := SizeOf(TSecurityAttributes);
    binherithandle      := true;
    lpsecuritydescriptor := nil;
  end;
  if Createpipe (ReadPipe, writePipe, @SecAttrib, 0) then begin
    CaptureBuffer := AllocMem(CaptureBufferSize + 1);
    FillChar(Startup,Sizeof(Startup),#0);
    Startup.cb         := SizeOf(Startup);
    Startup.hStdOutput := writePipe;
    Startup.hStdInput  := ReadPipe;
    Startup.dwFlags    := STARTF_USESTDHANDLES +
                           STARTF_USESHOWWINDOW;
//    Startup.wShowWindow := SW_HIDE;
    Startup.wShowWindow := SW_NORMAL;
    if CreateProcess(nil, PChar(command), @SecAttrib, @SecAttrib, true, NORMAL_PRIORITY_CLASS, nil, nil, Startup, ProcessInfo) then begin

      repeat
        WaitHandle := WaitForSingleObject( ProcessInfo.hProcess,30);
        Application.ProcessMessages;
      until (WaitHandle <> WAIT_TIMEOUT) or application.terminated;

      if not application.terminated then begin
        Repeat
          BytesRead := 0;
          ReadFile(ReadPipe,CaptureBuffer[0],CaptureBufferSize,BytesRead,nil);
          CaptureBuffer[BytesRead]:= #0;
          OemToAnsi(CaptureBuffer,CaptureBuffer);
          OutputMemo.Text := OutputMemo.Text+String(CaptureBuffer);
        until (BytesRead < CaptureBufferSize);
      end
      else
        OutPutMemo.add('# Operation canceled!');

      FreeMem(CaptureBuffer);
      CloseHandle(ProcessInfo.hProcess);
      CloseHandle(ProcessInfo.hThread);
      CloseHandle(ReadPipe);
      CloseHandle(writePipe);
    end
    else
      OutPutMemo.add('# cannot create process. Error: #'+inttostr(getlasterror));
  end
  else
    OutPutMemo.add('# cannot create pipe. Error: #'+inttostr(getlasterror));
end;

shmia 19. Jul 2004 18:18

Re: Problem mit WaitForSingleObject
 
Zitat:

Zitat von Nightshade
Ich hab da ein kleines Problem :

Wenn ich die untenstehende Procedure (Hab ich hier aus dem Forum) aufrufe bleibt er in :

Delphi-Quellcode:
     repeat
        WaitHandle := WaitForSingleObject( ProcessInfo.hProcess,30);
        Application.ProcessMessages;
      until (WaitHandle <> WAIT_TIMEOUT) or application.terminated;

WaitForSingleObject:
If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError.
Delphi-Quellcode:
repeat
  WaitHandle := WaitForSingleObject( ProcessInfo.hProcess,30);
  if WaitHandle=WAIT_FAILED then  // <---------
     RaiseLastWin32Error;
  Application.ProcessMessages;
  until (WaitHandle <> WAIT_TIMEOUT) or application.terminated;
Du solltest auch Resourceschutzblöcke für deine Handles verwenden.

Nightshade 20. Jul 2004 13:36

Re: Problem mit WaitForSingleObject
 
Zitat:

Zitat von shmia
WaitForSingleObject:
If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError.
Delphi-Quellcode:
repeat
  WaitHandle := WaitForSingleObject( ProcessInfo.hProcess,30);
  if WaitHandle=WAIT_FAILED then  // <---------
     RaiseLastWin32Error;
  Application.ProcessMessages;
  until (WaitHandle <> WAIT_TIMEOUT) or application.terminated;
Du solltest auch Resourceschutzblöcke für deine Handles verwenden.

Wenn die Function was anderes als "WAIT_TIMEOUT" zurückliefern würde, würde er nicht in der Schleife hängen bleiben.
Nee, Der Process, der erschaffen wird, hängt sich weg. (Denke ich)


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:05 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz