Solved

Check if another instance of appication is running

Posted on 2002-06-09
4
230 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now