Board index » delphi » waiting for a WinExec'd program to end

waiting for a WinExec'd program to end

Hi folks.

This is a Delphi 1.0 question.

I'm using WinExec to run a DOS program and then loading its piped
results into a tmemo.

Unfortunately, the app runs so fast that sometimes it loads the file
into the tmemo before it's finished being filled by the run of the DOS
program.

Instead of putting in a delay of, say one second or a half-second, I
want to make sure this file is ready for viewing, period.  

Does anyone know of how to wait for the completion of execution of an
application spawned using either WinExec or some other method?

 

Re:waiting for a WinExec'd program to end


Quote
> Van: Paul Munn <pm...@munn.com>
> Nieuwsgroepen: comp.lang.pascal.delphi.misc
> Onderwerp: waiting for a WinExec'd program to end
> Datum: vrijdag 24 januari 1997 6:38

> Does anyone know of how to wait for the completion of execution of an
> application spawned using either WinExec or some other method?

Hello Paul
here's the answer you might need:
Its a simple delphi procedure (only tested in D1.0) which spawns execution
of an application until
it's ended.

Function WinExecAndWait(Path:string;Visibility:Word):Word;
var InstanceID:THandle;
    PathLen:Integer;
begin
     PathLen:=Length(Path);
     Move(Path[1],Path[0],PathLen);
     Path[PathLen]:=#00;
     InstanceID:=WinExec(@Path,Visibility);
     if InstanceID<32 then
        WinExecAndWait:=InstanceID
     else begin
          Repeat
                Application.ProcessMessages;
          until Application.Terminated or (GetModuleUsage(InstanceID)=0);
          WinExecAndWait:=32;
     end;
end;

Hope it does what you want it to....

Bye
Allan B. Hofstede
<facts...@worldaccess.nl>
----------

Re:waiting for a WinExec'd program to end


Quote
Paul Munn wrote:

> Hi folks.

> This is a Delphi 1.0 question.

> Does anyone know of how to wait for the completion of execution of an
> application spawned using either WinExec or some other method?

try this:

var
  InstID: THandle
begin
  InstID := WinExec(<arguments>);
  while not (0 = GetModuleUsage(InstID) or Application.Terminated) do
    Application.ProcessMessages;
end;

Bye

Re:waiting for a WinExec'd program to end


Quote
Fabio wrote:

> Paul Munn wrote:

> > This is a Delphi 1.0 question.

> > Does anyone know of how to wait for the completion of execution of an
> > application spawned using either WinExec or some other method?

> try this:

> var
>   InstID: THandle
> begin
>   InstID := WinExec(<arguments>);
>   while not (0 = GetModuleUsage(InstID) or Application.Terminated) do
>     Application.ProcessMessages;
> end;

This doesn't work perfectly if you have a second dos app running,
at least not under win95.  ??

Jim

Re:waiting for a WinExec'd program to end


Quote
James Lee Tabor wrote:

> Fabio wrote:

> > Paul Munn wrote:

> > > This is a Delphi 1.0 question.

> > > Does anyone know of how to wait for the completion of execution of an
> > > application spawned using either WinExec or some other method?

> > try this:

> > var
> >   InstID: THandle
> > begin
> >   InstID := WinExec(<arguments>);
> >   while not (0 = GetModuleUsage(InstID) or Application.Terminated) do
> >     Application.ProcessMessages;
> > end;

> This doesn't work perfectly if you have a second dos app running,
> at least not under win95.  ??

> Jim

Use CreateProcess() and then wait on the process ID using
WaitForSingleObject()

BOOL CreateProcess(
    LPCTSTR  lpApplicationName, // pointer to name of executable module
    LPTSTR  lpCommandLine,      // pointer to command line string
    LPSECURITY_ATTRIBUTES  lpProcessAttributes, // pointer to process
security attributes
    LPSECURITY_ATTRIBUTES  lpThreadAttributes,  // pointer to thread
security attributes
    BOOL  bInheritHandles,      // handle inheritance flag
    DWORD  dwCreationFlags,     // creation flags
    LPVOID  lpEnvironment,      // pointer to new environment block
    LPCTSTR  lpCurrentDirectory,        // pointer to current directory name
    LPSTARTUPINFO  lpStartupInfo,       // pointer to STARTUPINFO
    LPPROCESS_INFORMATION  lpProcessInformation         // pointer to
PROCESS_INFORMATION  
   );

LPPROCESS_INFORMATION has the stuff you need to wait.

typedef struct _PROCESS_INFORMATION { // pi
    HANDLE hProcess;
    HANDLE hThread;
    DWORD dwProcessId;
    DWORD dwThreadId;

Quote
} PROCESS_INFORMATION;

Use the hProcess handle in the call to:

DWORD WaitForSingleObject(
    HANDLE  hObject,    // handle of object to wait for
    DWORD  dwTimeout    // time-out interval in milliseconds  
   );

NOTE: you can also control the new process's main thread because you
have the HThread Handle so you can Suspend and resume the tread (you can
also Terminate both the tread and the process but this is a bit
dangerous -- see the online help)
--
Barton L. Phillips                        http://www.applitec.com
Applied Technology Resources, Inc.
9045 Corbin Ave., Suite B2
Northridge, CA 91324-3300

Other Threads