How to ensure that I only have one instance of app running?

Hi, is there a simple and slick way to ensure that I only have one instance of my app running at a time?

Thanks!
   Shawn
shawn857Asked:
Who is Participating?
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.

Marco GasiFreelancerCommented:
You can use Jcl (www.delphi-jedi.org/¿): in the source of your project put

uses
  JclAppInst;

begin
  JclAppInstances.CheckSingleInstance; // Added instance checking
  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm);
  Application.Run;
end;
0
MerijnBSr. Software EngineerCommented:
Additionally you could add functionality that the already running version is being put to front when you start a second time, I think this is not in the jcl implementation.
0
Ephraim WangoyaCommented:
Try this


program.dpr;

...............

function CheckRunning: Boolean;
var
  Handle: THandle;
begin
  Handle := FindWindow('TApplication', Your Application Title'');
  Result :=  Handle <> 0;
  if Result then
	Windows.ShowWindow(AHandle, SW_RESTORE);
end;

begin
  
  if CheckRunning then
  begin
    Application.Terminate;
    Exit;
  end;
 
  Application.Initialize;
  .........

Open in new window

0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

shawn857Author Commented:
Ewangoya, I tried your code, but first it gave an error that it couldn't find 'Findwindow', so I aded unit Windows (is that correct?). Then it gave an error that variable AHandle wasn't decalred, so I included it in the declaration along with Handle: THandle;
   Finally, after clearing up all the compile errors, the code doesn't work. I can't even run my application at all... not even one instance. It just won't run.

Regards
    Shawn
0
Marco GasiFreelancerCommented:
If you don't want use Jcl try to download the attached file and add it to your uses clause.

Cheers
MultInst.pas
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
MugahiedCommented:
0
Ephraim WangoyaCommented:
Sorry, there is no AHandle

function CheckRunning: Boolean;
var
  Handle: THandle;
begin
  Handle := FindWindow('TApplication', 'Your Application Title');
  Result :=  Handle <> 0;
  if Result then
      Windows.ShowWindow(Handle, SW_RESTORE);
end;
0
shawn857Author Commented:
That worked fantastic Marqus, thanks a million!

Cheers
   Shawn
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.