user2524670
user2524670

Reputation: 277

How can I wait until an external process has completed?

So I have made a form in Delphi that allows me to run various sikuli scripts with various configurations. What I am working on now is to make it possible for someone using the form, to set up various sikuli scripts to run on after another. As in:

Step 1: SikuliScript1 with ConfigFile1. Step 2: SikuliScript2 with ConfigFile2. etc... This is my code so far:

procedure TSikRunForm.btnRunClick(Sender: TObject);
begin
    DirCombo:= '/C '+DirSik+'\sikuli-script.cmd' +' -r ' + DirScript + ' --args '+DirConfig;
    if SikFound then begin
      ShellExecute(Handle, nil, 'cmd.exe', pChar(DirCombo), nil, SW_SHOWNORMAL);
      Application.minimize;
    end else begin
      ShowMessage('Select the correct folder for your Sikuli installation folder');
    end;
end;

And this works perfect, the sikuli script runs perfectly, and while running, the cmd line is visible with the various actions shown that are being performed. After the sikuli script is done, the cmd line closes on its own. So the shell handler knows when to shut down the running process. So my question is: Is it possible to tell delphi: After the handler has shut the process, run the next process (Sikuli Script)? Now I know I can go with the whole createProcess in delphi, but it just seems overkill. There has got to be a way to do this faster and easier. Anyone have a clue?

Upvotes: 15

Views: 40451

Answers (4)

user2524670
user2524670

Reputation: 277

Ok I've looked more into this and have solved it, just as David suggested. I'll show what I did, should anyone else have the some questions or anything:

procedure TSikRunForm.btnRunClick(Sender: TObject);
begin
CmdLine:=  'C:\\windows\\system32\\cmd.exe';
uniqueString(CmdLine);
if SikFound then begin
  //----------For loop---------------
  DirScript:= TScriptEdit.Text;
  DirConfig:= TConfEdit.Text;
  DirCombo:= '/C '+DirSik+'\sikuli-script.cmd' +' -r ' + DirScript + ' --args '+DirConfig;
  CreateProcess(PChar(CmdLine),PChar(DirCombo),Nil,Nil,False,CREATE_NO_WINDOW,nil,nil,StartInfo,ProcInfo);
  procHandle:= ProcInfo.hProcess;
  Application.minimize;
  WaitForSingleObject(procHandle, INFINITE);

  DirScript:= TScriptEdit2.Text;
  DirConfig:= TConfEdit2.Text;
  DirCombo:= '/C '+DirSik+'\sikuli-script.cmd' +' -r ' + DirScript + ' --args '+DirConfig;
  CreateProcess(PChar(CmdLine),PChar(DirCombo),Nil,Nil,False,CREATE_NO_WINDOW,nil,nil,StartInfo,ProcInfo);
  procHandle:= ProcInfo.hProcess;
  Application.minimize;
  WaitForSingleObject(procHandle, INFINITE);
  //----------------------------------------
  showMessage('Gedoan');
end else begin
  ShowMessage('Select the correct folder for your Sikuli installation folder');
end;
end;

Works exactly as I wanted. The 2 sikuli scripts will run one after another.

Upvotes: 2

Martin
Martin

Reputation: 615

This is a hack and way less sophisticated, but maybe you could just generate a *.bat file containing the calls to the scripts, if necessary using "start /wait" on each line.

Upvotes: 1

Bart van Dijk
Bart van Dijk

Reputation: 486

With CreateProcess you can get a process handle and with WaitForSingleObject you can check when the process is complete. I use the following function, this runs the command in the background:

procedure ExecuteAndWait(const aCommando: string);
var
  tmpStartupInfo: TStartupInfo;
  tmpProcessInformation: TProcessInformation;
  tmpProgram: String;
begin
  tmpProgram := trim(aCommando);
  FillChar(tmpStartupInfo, SizeOf(tmpStartupInfo), 0);
  with tmpStartupInfo do
  begin
    cb := SizeOf(TStartupInfo);
    wShowWindow := SW_HIDE;
  end;

  if CreateProcess(nil, pchar(tmpProgram), nil, nil, true, CREATE_NO_WINDOW,
    nil, nil, tmpStartupInfo, tmpProcessInformation) then
  begin
    // loop every 10 ms
    while WaitForSingleObject(tmpProcessInformation.hProcess, 10) > 0 do
    begin
      Application.ProcessMessages;
    end;
    CloseHandle(tmpProcessInformation.hProcess);
    CloseHandle(tmpProcessInformation.hThread);
  end
  else
  begin
    RaiseLastOSError;
  end;
end;

Upvotes: 25

David Heffernan
David Heffernan

Reputation: 612964

You need to wait until the process handle is signaled. For example by a call to WaitForSingleObject. Obviously CreateProcess returns you a process handle on which you can wait. You cannot persuade ShellExecute to return a process handle, but its more capable brother ShellExecuteEx will do so.

However, I personally would choose CreateProcess here. You will get more flexibility and control, and it's not that complex. For example, you can suppress display of the console window if you wish.

Upvotes: 7

Related Questions