Link to home
Start Free TrialLog in
Avatar of roosiedb
roosiedb

asked on

Check if another instance of appication is running

Hi there,

To prevent database problems, I would like to avoid that 2 (or more) instances of my application are running at the same time.

So my question is:
How can I check if another instance is running and, if there is indeed, terminate the program. Note: only the "second" instance must be terminated of course.

Thank you in advance.
RoosieDB
Avatar of marcoszorrilla
marcoszorrilla

How to avoid the execution of double instances of a program.  
 
One of the easier ways of avoiding double instances of a program it is by means of ' atoms'. An atom is like an entrance in a list where we can hang certain information, for example that our program is in execution. The first thing that it is necessary to make then is, when pulling up the application to verify that present is not already, in the event of it is in memory it is warned to the user of their it misleads and he closes the instance. The routine to verify the presence of our application could be something like that:

function TFrom1.InstanciaPrevia: Boolean; var AtomText : array[0..30] of char; FoundAtom : TAtom; begin StrFmt(AtomText, 'JustOne%s', ['Nombre Aplicación']); FoundAtom := GlobalFindAtom(AtomText); Result := (FoundAtom <> 0); if Not Result then GlobalAddAtom(AtomText); end;

Evidently, then when closing the program it is necessary to remove the label that warns us that we are in memory, for it has enough it with coding the following thing:


function TFrom1.QuitarMarcadeInstancia: Boolean;
var       AtomText  : array[0..30] of char;
          FoundAtom : TAtom;
begin
  StrFmt(AtomText, 'JustOne%s', ['Nombre Aplicación']);
  FoundAtom := GlobalFindAtom(AtomText);
  if FoundAtom <> 0 then
     GlobalDeleteAtom(FoundAtom);
end;

Best Regards.
Marcos

Avatar of roosiedb

ASKER

Hi,

I was hoping that something like:

If "program already running" then close;

in the OnCreate event would do the job...


Anyway, I will test your solution soon.

gr.,
RoosieDB
ASKER CERTIFIED SOLUTION
Avatar of Lee_Nover
Lee_Nover

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Working OK.
Simple and good.

thanx.