Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Wait for another application to finnish a process

Posted on 2004-04-29
8
Medium Priority
?
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

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 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

722 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