Einzelnen Beitrag anzeigen

Benutzerbild von thkerkmann
thkerkmann

Registriert seit: 7. Jan 2006
Ort: Pulheim Brauweiler
464 Beiträge
 
Delphi 2010 Professional
 
#3

Re: Probleme mit WaitForSingleObject

  Alt 21. Dez 2006, 16:05
Hi,

versuchs mal so:

Delphi-Quellcode:
function tForm1.WinExecAndWait32(const Path: string; Visibility: Word): Boolean;
var
  lpExitCode: Cardinal;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  bIsSetup: boolean;
  iCnt: integer;
  iFind: integer;
begin
  bIsSetup := pos('setup.exe', lowercase(path)) > 0;
  { clear StartupInfo }
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);

  { enter startup information }
  with StartupInfo do
  begin
    cb := SizeOf(TStartupInfo);
    dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
    wShowWindow := visibility; {you could pass sw_show or sw_hide as parameter}
  end;

  { create new process }
  if CreateProcess(nil, pChar(path), nil, nil, False, NORMAL_PRIORITY_CLASS,
    nil, nil,
    StartupInfo, ProcessInfo) then
  begin
    { sucess }
    WaitForInputIdle(ProcessInfo.hProcess, 3000);
    { wait until application finished }
    repeat { until lpExitCode <> Still_Active }
      sleep(0);
      application.processmessages;
      GetExitCodeProcess(ProcessInfo.hProcess, lpExitCode);
    until lpExitCode <> Still_Active;

    with ProcessInfo do
      {not sure this is necessary but seen in in some code elsewhere}
    begin
      CloseHandle(hThread);
      CloseHandle(hProcess);
    end; { with ProcessInfo do }

end;
Das verwende ich schon lange und es funktioniert sehr zuverlässig.

Gruss
Thomas Kerkmann
Ich hab noch einen Koffer in Borland.
http://thomaskerkmann.wordpress.com/
  Mit Zitat antworten Zitat