Solved

Check if another instance of appication is running

Posted on 2002-06-09
4
233 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:roosiedb
  • 2
4 Comments
 
LVL 3

Expert Comment

by:marcoszorrilla
ID: 7065430
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

0
 

Author Comment

by:roosiedb
ID: 7065439
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
0
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 50 total points
ID: 7065467
he showed you a way with Atoms
I use Semaphores ... you could also use Mutexes or MemoryMappedFiles .. lots of way to do it :)

anyway here's the code I use
I have this in the project file (.dpr)




uses windows;

var hSem: Cardinal;

begin
     // allow only one instance of the server
     hSem:=CreateSemaphore(nil, 0, 1, 'SEM_lnVidServGuard');
     if (hSem > 0) and (GetLastError = ERROR_ALREADY_EXISTS) then
     begin
       CloseHandle(hSem);
       exit;
     end;

     with Application do
     begin
       Initialize;
       Title:='VideoServer Guard';
       HelpFile:=AppHelpFile;
       ShellExecute(0, nil, PChar(AppPath + 'vsgDBServer.exe'), nil, nil, SW_HIDE);
       CreateForm(TGuardF, GuardF);
       Run;
       CancelHint; // release the hint timer
       CloseHandle(hSem);
     end;    

     { or a standard application.run section like  :)
     Application.Initialize;
     Application.CreateForm();
     Application.Run;
     }
end.
0
 

Author Comment

by:roosiedb
ID: 7065484
Working OK.
Simple and good.

thanx.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…

830 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