Solved

Wait for another application to finnish a process

Posted on 2004-04-29
8
422 Views
Last Modified: 2010-08-05
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');
.............................


0
Comment
Question by:hakanfa
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10947464
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10947512
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;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10947547
example about SPath: 'C:\INSTALL\INTERBASE\Server\'
      lpFile := PChar(SPath + SFile);
if SPath: 'C:\INSTALL\INTERBASE\Server'
then
      lpFile := PChar(SPath + '\'+ SFile);
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10947665
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;
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:hakanfa
ID: 10948114
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

0
 
LVL 17

Accepted Solution

by:
geobul earned 250 total points
ID: 10948126
Hi,

1. You have a message handler where you're changing the value of 'i' from 0 to 1:
while i = 0 do begin{-> i is the parameter the changes when you get the answer from server}
  Application.ProcessMessages;
  Sleep(10);
end;
...

2. Without additional message handler and 'i' variable
var Msg: TMsg;
...
  ShellExecute(0, 'open', PChar(serverfilename), NIL, NIL, SW_NORMAL);
  Sleep(500);
  while true do begin
    GetMessage(Msg, 0, 0, 0);
    if Msg.message = MyMessage then break; // MyMessage is the message number/ID you are waiting for
    TranslateMessage (Msg);
    DispatchMessage (Msg);
  end;

You might want to register your own message number system-wide in both applications using this code in both apps:

var MyMessage: DWORD;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyMessage := RegisterWindowMessage('MyVerySecretMessage'); // MyMessage will have the same value in both apps
end;

and send/post that message in the called app to the calling app.

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 10948199
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;
0
 

Author Comment

by:hakanfa
ID: 10971963
Thanks geobul, thats the way to do it! Works fine!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now