Thema: Delphi ExecAndWait

Einzelnen Beitrag anzeigen

Benutzerbild von spacewolf
spacewolf

Registriert seit: 24. Apr 2003
Ort: Magdeburg
218 Beiträge
 
Delphi 7 Professional
 
#1

ExecAndWait

  Alt 3. Mai 2009, 11:09
Hallo wir haben die allgemeingültige ExecAndWait etwas anders gestaltet... (so mit Timeouts usw..)

Delphi-Quellcode:
function ExecAndWait(const AFileName, AParams: String; AShowWindow: Word;
  AWarten: Boolean; AUserName, ADomain, APass: String; AMoniIdx: Integer;
  AWindowName: String; var AExitCode, APId: DWord;
  const ATimeOut: Integer = 0; const ASetForegroundWnd: Boolean = False): Boolean;
var
  SEInfo: TShellExecuteInfo;
  LProcInfo: TProcessInformation;
  Path: String;
begin
  Result := False;

  if GDaProtCreated then
    AddDo('AFileName AParams: ' + AFileName + ' ' + AParams);

  try
    try
      if ExtractFileDir(AFileName) <> 'then begin
        GetDir(0, Path);
        ChDir(ExtractFileDir(AFileName));
      end else
        GetDir(0, Path);
    except end;
    try
      if AUserName <> 'then begin
        //Start Process with Logon - raises Exception if fails
        daCreateProcessWithLogonW(AUserName, ADomain, APass, AFileName, AParams,
          AShowWindow, @LProcInfo);
        SetToMoni(LProcInfo.hProcess);
        WaitForApp(LProcInfo.hProcess);
        CloseHandle(LProcInfo.hProcess);
        CloseHandle(LProcInfo.hThread);
        Result := True;
      end else begin
        FillChar(SEInfo, SizeOf(SEInfo), 0);
        SEInfo.cbSize := SizeOf(TShellExecuteInfo);
        with SEInfo do begin
          fMask := SEE_MASK_NOCLOSEPROCESS OR SEE_MASK_FLAG_NO_UI;
          Wnd := Application.Handle;
          lpFile := PChar(AFileName);
          if AParams = 'then
            lpParameters := nil
          else
            lpParameters := PChar(AParams);
          nShow := AShowWindow;
        end;
        if ShellExecuteEx(@SEInfo) then
        begin
          // nur zum Test:
          if aWarten then
            WaitForSingleObject(SEInfo.hProcess,INFINITE);

          WaitForApp(SEInfo.hProcess);
          CloseHandle(SEInfo.hProcess);
          Result := True;
        end else begin
          Result := False;
          RaiseLastOSError;
        end;
      end;//ELSE OF if AUserName <> '' then begin
    finally
      try
        ChDir(Path);
      except end;
    end;
  except
    on e:Exception do
      daShowMessage(Format(SPMEErrRunProcess, [ExtractFileName(AFileName)])+#13#10#13#10+
                    SPMErrMsg+ ': '+e.Message+#13#10#13#10+
                    SPMPath+': '+AFileName+#13#10+
                    SPMParams+': '+AParams+#13#10+
                    SPMStatus+': '+IntToStr(AShowWindow)+#13#10+
                    SPMErrCls+': '+e.ClassName);
  end;
end;
In dieser Methode wird ein WaitForApp aufgerufen:

Delphi-Quellcode:
  procedure WaitForApp(const AHdlProcess: Cardinal);
  var
    Window: Cardinal;
    LStart: TDateTime;
  begin
    if AWarten then begin
      LStart := Now;
      if ASetForegroundWnd then
        Window := GetActiveWindow;

      repeat
        Sleep(100);//2004_12_15 to prevent 100% CPU-Usage
        Application.ProcessMessages;
        GetExitCodeProcess(AHdlProcess, AExitCode);
        if (AExitCode <> STILL_ACTIVE) OR Application.Terminated then
          Break;
        //2009.04.05 MET: ATimeOut-Handling
        if (ATimeOut <> 0) AND (Now - LStart >= ATimeOut) then begin
          AExitCode := STATUS_TIMEOUT;
          Break;
        end;
      until True;

      if ASetForegroundWnd then
        SetForegroundWindow(Window);
    end;
  end;
Dieses WaitForApp aber wiederum funktioniert nicht so wie geplant, scheint aus der Funktion herauszuspringen bevor das Programm sich beendet:

Wenn ich nach alter Manier ein: WaitForSingleObject - einfüge funktioniert alles wieder.

So nun meine Frage, kann jemand im WaitForApp - etwas erkennen was da falsch gedacht ist?

der

Andreas
Andreas Göllner
("`-''-/").___..--''"`-._
`6_ 6 ) `-. ( ).`-.__.`)
"Ich kann Dir nur die Tür zeigen, durchgehen musst Du ganz allein."
Wer ist die Tür? Jesus!
  Mit Zitat antworten Zitat