Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 02-21-2002, 10:31 AM
Beardawg Beardawg is offline
Member
 
Join Date: Mar 2001
Posts: 75
Default Waiting for application to finish...

I have an application that is launced from a command line several times a minute... How do I force the next iteration to "wait" till the one before it has finished... I know how to do this in Ada and C... Do I create a Handle, check for it, waituntil not found? Something like this? I usually write programs with UI's, this one's a bit different...

Any help?

Many thanks!
Reply With Quote
  #2  
Old 02-21-2002, 10:38 AM
HTML HTML is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,977
Default RE: Waiting for application to finish...

Hello,

Here is the very popular code from Peter Below (TeamB).


{-- WinExecAndWait32V2 ------------------------------------------------}
{: Executes a program and waits for it to terminate
@Param FileName contains executable + any parameters
@Param Visibility is one of the ShowWindow options, e.g. SW_SHOWNORMAL
@Returns -1 in case of error, otherwise the programs exit code
@Desc In case of error SysErrorMessage( GetlastError ) will return an
error message. The routine will process paint messages and messages
send from other threads while it waits.
}{ Created 27.10.2000 by P. Below
-----------------------------------------------------------------------}
Function WinExecAndWait32V2( FileName: String; Visibility: integer ):
DWORD;
Procedure WaitFor( processHandle: THandle );
Var
msg: TMsg;
ret: DWORD;
Begin
Repeat
ret := MsgWaitForMultipleObjects(
1, { 1 handle to wait on }
processHandle, { the handle }
False, { wake on any event }
INFINITE, { wait without timeout }
QS_PAINT or { wake on paint messages }
QS_SENDMESSAGE { or messages from other threads }
);
If ret = WAIT_FAILED Then Exit; { can do little here }
If ret = (WAIT_OBJECT_0 + 1) Then Begin
{ Woke on a message, process paint messages only. Calling
PeekMessage gets messages send from other threads processed. }
While PeekMessage( msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE ) Do
DispatchMessage( msg );
End;
Until ret = WAIT_OBJECT_0;
End; { Waitfor }
Var { V1 by Pat Ritchey, V2 by P.Below }
zAppName:array[0..512] of char;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
Begin { WinExecAndWait32V2 }
StrPCopy(zAppName,FileName);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
If not CreateProcess(nil,
zAppName, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) { pointer to PROCESS_INF }
Then
Result := DWORD(-1) { failed, GetLastError has error code }
Else Begin
Waitfor(ProcessInfo.hProcess);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle( ProcessInfo.hProcess );
CloseHandle( ProcessInfo.hThread );
End; { Else }
End; { WinExecAndWait32V2 }


procedure TForm1.Button1Click(Sender: TObject);
begin
WinExecAndWait32V2( 'notepad.exe', SW_SHOWNORMAL );
end;
Reply With Quote
  #3  
Old 02-21-2002, 12:48 PM
Beardawg Beardawg is offline
Member
 
Join Date: Mar 2001
Posts: 75
Default RE: Waiting for application to finish...

Well, this is a formless application and does that halt the entire system? Essentially here's my scenario... I have an application monitoring a bldg... When an event occurs, it launches my application(in the background) and does some things... Well, say someone badgeds in at a door, lights are turned on in the lobby, and the heat in the basement all occuring within seconds of each other all calling my application, I need each instance of my application to "wait" on the other...
Reply With Quote
  #4  
Old 02-21-2002, 01:06 PM
HTML HTML is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,977
Default RE: Waiting for application to finish...

Hello,

To prevent the call from halting your program you could put it into a seperate application thread, Instead of the main thread which it is in right now.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:22 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.