Board index » delphi » Checking for another instance of app?

Checking for another instance of app?

When my Delphi application starts up, how can I check if another
instance of my app is already running? I would like to have
only one instance at a time.

Thanks in advance,

Antti

 

Re:Checking for another instance of app?


HI there,

You could do two things (both are done in your Project source *before*
any createForm calls, etc - i.e. right after the initial BEGIN):

1) Check the value of HPrevInst (see the Help file for info):

[snip]

   if HPrevInst <> 0 then
     Halt;
[snip]

2) If your application has a *unique* Window Title, and you want to be
"fancy" ;-), you could pop up the active copy of your program:

[snip]
  var
    Wnd: HWnd;

[snip]

   Wnd:=FindWindow(nil,"My Unique Window Title");
   if Wnd <> 0 then
     begin
       if IsEnabled(Wnd) then
         if IsIconic(Wnd) then
           OpenIcon(Wnd)
         else
           BringWindowToTop(Wnd);
     end;

[snip]

Hope this helps...

Cheers,
G.--- _Snappy_

Other Threads