Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wait for process to end 1

Status
Not open for further replies.

earlrainer

Programmer
Mar 1, 2002
170
IN
Hi all,
I've got into a teeny weeny problem.

I execute a lot of batch files through my delphi program.
my problem is that I have to call the batch files in a particular order..meaning I first call batchfile1.then i have to wait till it is over ,then i do some processing and then i call batchfile2...and so on(I use winexec to run the bachfiles).
I use the following method..I create a temporary file c:\a.txt before I call batchfile1.
in batchfile1 I delete this file at the end.

in my delphi program i write like this
while fileexists('c:\a.txt') do
application.processmessages;

I have 1 problem due to this..
somehow this method seems to take too much resources.
the program hangs while in the loop.

is there a more efficient way to do this.

Bye
 
earlrainer,

Are you using WinExec to call successive batch files? If so, you might consider calling creating a single batch file that executes the others using CALL. For example:

Code:
   call batch1.bat
   call batch2.bat
   call batch3.bat

Also, you should probably replace your call to WinExec with one that uses either ShellExecute or createProcess to wait for the spawned process to continue.

As an example, here's one that I've used from time to time, based on some code posted by Pat Richey to various newsgroups:

Code:
 function execWait( strFile, strPath, strOpts : String ) :
 Boolean;
 { ---------------------------------------------------------------
   Executes an external program and waits for it to exit.  The
   parameters are:

   -- strFile: The name of the program to execute
   -- strPath: The directory to use.
   -- strOpts: Any command line options needed.

   Return value indicates if the application executed and
 completed 
   successfully (TRUE).  If not, FALSE is returned.  (Note full 
   error handling deleted for this posting.)

   Please note that this implementation is based on Pat Richey's
   WinExecAndWait32, as posted to several newsgroups over the past
   few years; for more info, search groups.google.com
   ---------------------------------------------------------------
 }
 var
   si : tStartupInfo;         { Startup info for spawned process }
   pi : tProcessInformation;  { Process handle for spawned process
 }
   dwRetval : dWORD;          { Return value from createProcess }
 begin

   { Initialize the startup info record }
   fillChar( si, sizeOf( si ), #0 );
   with si do
   begin
      cb := Sizeof( StartupInfo );
      dwFlags := STARTF_USESHOWWINDOW;
      wShowWindow := sw_ShowNormal;
   end; { with }

   { try to launch the application }
   if not createProcess( nil, pchar( strPath + strFile + ' ' +
 strOpts ), nil,
			 nil, FALSE, CREATE_NEW_CONSOLE or
 NORMAL_PRIORITY_CLASS,
			 nil, pchar( strPath ), si, pi ) then
      begin
	 dwRetval := getLastError;  { save system error info }
	 // error handling deleted for brevity; see online help.
	 result := FALSE;  { trigger offer to cancel remaining
 items }
      end
   else
      begin
	 repeat
	    application.processMessages;  { wait }
	 until ( WaitforSingleObject( pi.hProcess, 500 ) <>
	         WAIT_TIMEOUT );
         { the following saved for future use }
	 GetExitCodeProcess( pi.hProcess, dwRetval ); 
	 CloseHandle( pi.hProcess );  { clean up }
	 CloseHandle( pi.hThread );
	 result := TRUE;
      end;
 end;

Note that a) I ripped out the error response code, which basically displayed an error do the user with some additional debugging information and b) it executes and *waits* for the target program to exit.

This will likely be more efficient, since WinExec remains in the API solely for 16-bit compatibility. Microsoft has long said that we shouldn't be using WinExec any longer.

For more information, please see:

--
--
And other related documents in the Platform SDK documentation. (Remember that Delphi provides Help files for many of these issues.)

Hope this helps...

-- Lance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top