how to close an application if it is running before running the application delphi

I have a delphi form that may be launched from different locations with different filters depending on where the user is coming from. Problem is when they launch if from LocationA and have it open then launch it from LocationB (which should show different groups and filters in the cxGrid) all it does is bring the already open form to front. How can I first check if its already open and if it is, close/terminate it THEN create the form?

Thanks!
if bRunApp then begin

      frmSplashScreen.WriteStatusLine('Loading frmDesEngReport...');
      AppLog.Msg := 'Loading frmDesEngReport...';
      frmSplashScreen.ProgressBar.StepIt;
      frmSplashScreen.Update;
      Application.CreateForm(TfrmDesEngReport, frmDesEngReport);
      Application.CreateForm(TdmDesEngReport, dmDesEngReport);
      frmSplashScreen.Hide;
      frmSplashScreen.Free;
      Application.Run;
    end

Open in new window

Norm-alNetwork EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andrevanzuydamCommented:
I used this code in an application that could only have one instance of itself running at any time.
Change the TMyApp to your main form class and the MyApp to your main form caption and give it a spin.

Uses Windows, ShellAPI; //Hope that is it

function EnumWindowsFunc(Handle: THandle; List: TStringList) : boolean ; stdcall;
var
  className: array[0..255] of Char;
  caption: array[0..255] of Char;
begin
  GetClassName(Handle, className, SizeOf(className)-1) ;
  GetWindowText(Handle, caption, SizeOf(caption)-1) ;

  if ((className = 'TMyApp') AND (caption = 'MyApp')) then
     List.Add(IntToStr(Handle)) ;

  Result :=True;
end;

procedure KillClones;
var
  cloneH : THandle;
  clones : TStringList;
  sHandle : string;
  iCount : Integer;
begin
  clones := TStringList.Create;
  try
    EnumWindows(@EnumWindowsFunc, LParam(clones));
    for iCount := 0 to clones.Count-1 do
    begin
      cloneH := StrToInt(clones.Strings[iCount]);
      if cloneH <> Self.Handle then
        SendMessage(cloneH, WM_CloneQuit, 0, 0);
    end;
  finally
    clones.Free;
  end;
end;             

Open in new window

0
Ephraim WangoyaCommented:

Thats not the problem, seems you have in your program code that does not allow two instances of your program to run at the same time.

Thats the code you need to disable depending on your parameters.
0
systanCommented:
try this dpr procedure;
program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.ShowMainForm := False;  ////hides the mainForm and can show other childForm

  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Open in new window


in TForm1; with the Timer;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
show;
end;

Open in new window

0
jimyXCommented:
The description in the question body does not match the title of the question. The title says two applications but the body indicates one application and one Form being called from different locations.

So if I understand your question correctly you mean you have in your application one form which can be called from two locations within the same application, am I right? If yes, then:

When this form is called from locationA and it's kept open and when this same form is called from locationB and you need to check if its already open, and if it is, close/terminate it then recreate it:

To check if the form is showing you can use this code:
  If FormX.showing then    // or if FormX.Visible then
    begin
      // proper handling is required
    end;

Open in new window

FormX is the name of your Form that you are testing if it's showing.

For better handling you may approach this as follows:
  if assigned(FormX) then
    begin
      If FormX.showing then
        begin
          // Close/Destroy/Recreate or whatever proper handling you may require
        end
      else
        begin
          // Destroy/Recreate the form
        end;
    end
  else   // the form does not exist
    begin
      // Recreate the form
    end;

Open in new window


You need to show sample code on how you call this form and where/how do you create it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
systanCommented:
Why there are many speechless askers?  They don't participate, they don't comment, lets respect each other by communicating.  Glad to comment.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.