Board index » delphi » Waiting for WinExec to Finish

Waiting for WinExec to Finish

I am writing a front end program for a dos-based compiler tool, and
need to call the compiler program using WinExec then wait for it to
finish before proceeding.

How can I do this - delphi's help is a bit shady on the subject to say
the least :)

Cheers,

John Breen

 

Re:Waiting for WinExec to Finish


Quote
wom...@mad.scientist.com (John Breen) wrote:
>I am writing a front end program for a dos-based compiler tool, and
>need to call the compiler program using WinExec then wait for it to
>finish before proceeding.
>How can I do this - delphi's help is a bit shady on the subject to say
>the least :)

Delphi 1.0 or 2.0.  In 1.0, you need to peek message until the handle
count of the spawned process is 0.  In 2.0, you need to use
createprocess and have it wait until returned.

Jay Cole

My real address is @bgn.mindspring.com to prevent spam.

Re:Waiting for WinExec to Finish


I had the same problem.

In my application I run a external program that runs a simulation. With
WinExec I call a batch-program that runs the model and removes a temp-file
that I created in my application before I called WinExec. In the
application in a loop I constantly check the exsistance of the temp-file.
When it is gone the simulation is ready.

It is a sollution but I think there must be a better way.

John Breen <wom...@mad.scientist.com> wrote in article
<3389072a.5106...@news.cowan.edu.au>...

Quote
> I am writing a front end program for a dos-based compiler tool, and
> need to call the compiler program using WinExec then wait for it to
> finish before proceeding.

> How can I do this - delphi's help is a bit shady on the subject to say
> the least :)

> Cheers,

> John Breen

Re:Waiting for WinExec to Finish


dortmund, stardate 9387.67,

 JB> I am writing a front end program for a dos-based compiler tool, and
 JB> need to call the compiler program using WinExec then wait for it to
 JB> finish before proceeding.

 JB> How can I do this - delphi's help is a bit shady on the subject to say
 JB> the least :)

in case you are using Win32:
  use CreateProcess instead of WinExec. CreateProcess returns a Process  
Handle that you can wait for using WaitForSingleObject.

function ExecuteAndWait(App,CmdLine:string):integer;
var StartupInfo:TStartupInfo;
    ProcessInfo:TProcessInformation;
    s:array[0..MAX_PATH] of char;
begin
  GetStartupInfo(Startupinfo);

  ExpandEnvironmentStrings(pChar(App),@s,MAX_PATH);
  App := string(pChar(@s));

  //writeln('execute: '+App+' '+CmdLine);
  if CreateProcess(pChar(App),
                   pChar(App+' '+CmdLine),
                   nil,nil,false,0,nil,
                   pChar(GetCurrentDir),
                   StartupInfo,
                   ProcessInfo) then begin
    CloseHandle(ProcessInfo.hThread);
    WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess,result);
    CloseHandle(ProcessInfo.hProcess);
  end
  else raise Exception.CreateFmt('could not create process %s %s: %d',[App,CmdLine,GetLastError]);
end;

hope this helps
marc

-+- Let Joy and Innocence Prevail
## CrossPoint v3.11 ##

Re:Waiting for WinExec to Finish


Thanks everybody for the help.  I think you've given me the answer.

FWIW I'm programming in Delphi 2 Developer under Win 95.

Cheers,

JB

Other Threads