Solved

Wait for another application to finnish a process

Posted on 2004-04-29
8
430 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
LAN or WAN ? 11 92
Press three keys together and trigger a function 3 57
Firemonkey webbrowser scrollbars ? 1 41
Delphi problems to abort a printjob 9 21
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 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