Link to home
Start Free TrialLog in
Avatar of XNokia
XNokia

asked on

Prevent application runs twice..

Hi Experts, how can I do that ? So if user run my application at the second time, the last application will terminate immediately.
I want the code and an explaination 'cos I'm a new Delphian here... :)
I hope giving you 75 points is enough for this question.


Blurp slurp ...

XNokia 6110
ASKER CERTIFIED SOLUTION
Avatar of RBertora
RBertora
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lischke
Lischke

Hi XNokia,

do you only need to know whether a previous instance of your application is already running or do you want to pass command line parameters of the second instance to the first one before terminating the second instance?

If you don't need to pass command lines around then there are several easy posibilities. One is to register a global ID with the system on startup of the first instance and a check for this ID to know whether a specific instance is not the first one.

Use on startup (e.g. in the project source (*.dpr file)):

  MyAtom := GlobaFindAtom('MyUniqueString');

to check whether your ID has already been registered. If MyAtom is zero then the ID is not registered yet and the instance is the first one. In this case it calls

  MyAtom := GlobalAddAtom('MyUniqueString');

to register now the ID and on program termination you need to call:

  GlobalDeleteAtom(MyAtom);

If MyAtom returned from GlobalFindAtom is <> 0 then there's already a running instance and the second one needs to quit.


A second possibility to force only one instance is to use FindWindow. A starting application would look for its own main window before it is created in the starting application. If the window is already there then there's also an already running first instance and the window can be used to bring the first instance to front.

A third way is the use of a file mapping, which is very elegant and which can be used to pass command line parameters (my preferred solution).

Ciao, Mike
Avatar of XNokia

ASKER

Thanx to Rob and Mike.

Rob, your answer is long but good, so I can modified the code to not just send WM_CLOSE but my built-in message... That helps so much...

And to Mike, your GlobalAddAtom is good and effective... and the third way you're saying that I must use AssignFile(f,'temp.txt') to make a flag ( If I'm not wrong ? 'Cos dunno what you mean about file mapping ). But those help much...

And the points wil be ......






te teet teeet...










For Rob !! Cos you answered at the 1'st time ...
Congratulation you're awarded 75 points... heheheh ( just like quiz )


XNokia 9110 - Connecting Popeye
:-)) No, no, file mapping has nothing to do with AssignFile etc. See CreateFileMapping in the Win32.hlp. This maps a part of a file (even the system swap file) into the memory for direct (random) access...

Ciao, Mike