Solved

Check if another instance of appication is running

Posted on 2002-06-09
4
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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