Solved

Check if another instance of appication is running

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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