Someone was looking for some code to test if their application is
already running a while ago. I just got this in the email from
http://www.zdtips.com and it works alright, and looks pretty simple.

Don't forget to declare Atom: TAtom in the private section.

This week's Delphi Tip

Make sure your .exe only runs once!

The following code ensures that your .exe will only run once:

procedure TForm1.FormCreate(Sender: TObject);
begin
  {Searchs table to see if the program is already running}
  if GlobalFindAtom('PROGRAM_RUNNING') = 0 then
    { If not found then add it }
    atom := GlobalAddAtom('PROGRAM_RUNNING')
  else begin
    { If program is already running the show message and halt }
    MessageDlg('You have the program running all ready!!', mtWarning,
[mbOK], 0);
    Halt;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  {Removes the item from the table so it can be run again}
  GlobalDeleteAtom(atom);
end;


------------------------------------------------------------------------
---
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to