Author: Jonas Bilinkevicius
How can I prevent the user from running my program twice during the same Windows
session? I want to force the user to log into Windows again before my application
can be started a second time.
Answer:
A way to make a program only be able to run once in every session is to create a
unique global atom string on first run. Then on the following run, check if the
string exists, and not run the program if the string atom is present. For example:
1 2 procedure TForm1.FormShow(Sender: TObject);
3 var4 atom: Integer;
5 begin6 if (GlobalFindAtom('This_is_some_unique_text') = 0) then7 atom := GlobalAddAtom('This_is_some_unique_text')
8 else9 begin10 ShowMessage('This application can only be run once for every Windows Session.');
11 Close;
12 end;
13 end;