Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check if another instance of appication is running

Posted on 2002-06-09
4
Medium Priority
?
238 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 150 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

604 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