16,742
社区成员
发帖
与我相关
我的任务
分享
program Aabb;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Windows,
Dialogs,
SysUtils;
{$R *.res}
var mutex: THandle;
var flag_existence: Integer;
begin
Application.Initialize;
mutex := CreateMutex(nil, False, PChar(Aabb)); //建立互斥对象
flag_existence := GetLastError();
ShowMessage(IntToStr(mutex));
try
if (mutex = 0) then
begin
ShowMessage('已有相同实例在运行');
end else if (mutex <> 0) then
begin
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
finally
ReleaseMutex(mutex);
CloseHandle(mutex);
end;
end.
...
...
var
RvHandle:THandle;
begin
//防止程序重复运行
RvHandle:=FindWindow('Tfrm_Main', nil);
if RvHandle = 0 then
begin
Application.Initialize;
Application.CreateForm(Tfrm_Main, frmMain);
Application.Run;
end;
end.
var
hMutex: THandle;
const
TheName = 'abc';
begin
if OpenMutex(MUTEX_ALL_ACCESS, False, TheName) <> 0 then
begin
MessageBox(0, '本程式已有一实例在运行中', '提示', MB_OK);
Application.Terminate;
end else
begin
hMutex := CreateMutex(nil, False, TheName);
Application.Initialize;
....
end;