Link to home
Start Free TrialLog in
Avatar of hakanfa
hakanfaFlag for Finland

asked on

Wait for another application to finnish a process

Hello!

I've a small problem with an application that starts another application with
the following command: ShellExecute(0, 'open', PChar(serverfilename), NIL, NIL, SW_NORMAL);

The problem is that I need to get the "Client" (the one that starts the other program) to wait
for a message (Postmessage, Sendmessage) from the other program before continuing the
procedure. For the moment I have a While..do statement doing this but in the long run it is
not a good idea..I think..

Any ideas...?

My Code:

fServerHandle := FindWindow(NIL, 'Server');

  if (fServerHandle=0) then begin
    AutoShutDownServer := TRUE;

    // Starts it
    serverfilename := ExtractFilePath(Application.ExeName)+'\AutoStartServer.exe';
    ShellExecute(0, 'open', PChar(serverfilename), NIL, NIL, SW_NORMAL);

    Sleep(500);
    while i = 0 do {-> i is the parameter the changes when you get the answer from server}
    Application.ProcessMessages;
    if i = 1 then
    Showmessage ('OK') else
   ShowMessage('NOT OK');
.............................


Avatar of Ivanov_G
Ivanov_G
Flag of Bulgaria image

uses
  ........, ShellAPI;

function  TForm1.The_Process_Finished(SPath, SFile: string): Boolean;
var
  B:      Boolean;
  ShExInf:TShellExecuteInfo;
begin
  B := False;
  try
    FillChar(ShExInf, Sizeof(ShExInf), 0);
    with ShExInf do
    begin
      cbSize:= Sizeof(ShExInf);
      fMask := SEE_MASK_NOCLOSEPROCESS;
      Wnd := Handle;
      lpVerb := 'open';
      lpFile := PChar(SPath + SFile); // file name here
      lpParameters := nil;
      nShow := SW_SHOWNORMAL {SW_SHOWMAXIMIZED} ;
    end;
    if FileExists(ShExInf.lpFile) and ShellExecuteEx(@ShExInf) then
    begin
      while WaitForSingleObject(ShExInf.hProcess, 200) <> WAIT_OBJECT_0 do
        Application.ProcessMessages; // keeps your app from freezing
    end
    else
      ShowMessage(SysErrorMessage(GetLastError));
    CloseHandle(ShExInf.HProcess);
  B := True;
  finally
    Result := B;
  endl
end;
example about SPath: 'C:\INSTALL\INTERBASE\Server\'
      lpFile := PChar(SPath + SFile);
if SPath: 'C:\INSTALL\INTERBASE\Server'
then
      lpFile := PChar(SPath + '\'+ SFile);
Avatar of Ferruccio Accalai
My Version without using ShellAPI but only Windows.pas...

procedure TForm1.Button1Click(Sender: TObject);
function ExecApplication(APPName, CmdLine: String; ShowMode: DWord; WaitToExit: Boolean): DWord;
var StartInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
begin
      try
      FillChar(StartInfo, SizeOf(StartInfo), 0);
              StartInfo.cb:=SizeOf(StartInfo);
              StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
      StartInfo.wShowWindow:=ShowMode;
              if AppName <> '' then
            CreateProcess(PChar(APPName), PChar(CmdLine), nil, nil, False, 0,
                         nil, nil, StartInfo, ProcInfo)
              else
            CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0,
                         nil, nil, StartInfo, ProcInfo);
      if WaitToExit then
            WaitForSingleObject(ProcInfo.hProcess, INFINITE);
      GetExitCodeProcess(ProcInfo.hProcess, Result);
   finally
      CloseHandle(ProcInfo.hProcess);
      CloseHandle(ProcInfo.hThread );
   end;
end;
begin
if ExecApplication('AutoStartServer.exe'',ExtractFilePath(Application.ExeName),SW_SHOWNormal,True) = 0 then ShowMessage('Zero');
end;
Avatar of hakanfa

ASKER

Well, alot a answers here but I need something more. Do be more specific here's more details about the
project:

What I'm trying to do is a kind of Client/Server type of program. When you press the login button on the client
form it starts the "server" (I've got a Login form created on the server each time it starts) and the Login form
is shown. Depending on what the user logs in as (ex. Admin, Production etc.) the server sends back a param
(<> 0) to the client and depending on the "answer" the client execute different procedures:

Like..
var
i:integer;
TClientFor.bbLogin.Click(Sender: TObject);
begin
{1.Execute command that starts the "server"}
{2.Wait for variable "i" to change from 0, i.e. a message from server depending on what kind of user logs in)}
{3.Continues depending on what value "i" got from the server}  
end;

What I want to do is to find another solution to the "Do..While" in my first question.

Hokki

ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of geobul
geobul

Additions to 2. after reading your second comment:

Perhaps the login value is in wParam or lParam of the message you're receiving from the server. If that is the case then change:

if Msg.message = MyMessage then break;

to:

if Msg.message = MyMessage then begin
  i := Msg.wParam;
  break;
end;
Avatar of hakanfa

ASKER

Thanks geobul, thats the way to do it! Works fine!