Q:如何限制應用程式一次只執行一份 instance ?
A:參考下列程式碼:
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FAtom: ATOM;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
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 }
FAtom := GlobalAddAtom('PROGRAM_RUNNING')
else
begin
{ If program is already running the show message and halt }
ShowMessage('There is already an instance of this program running.');
Application.Terminate;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
GlobalDeleteAtom(FAtom);
end;
               (
geocities.com/huanlin_tsai)