Solved

Wait for another application to finnish a process

Posted on 2004-04-29
8
425 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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 is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

929 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

10 Experts available now in Live!

Get 1:1 Help Now