hMutex := CreateMutex(nil, False, '');
if (GetLastError() = ERROR_ALREADY_EXISTS) then
begin
Application.MessageBox('该程序已在运行!','请注意……',
MB_OK + MB_ICONWARNING);
Exit;
end;
if (GetLastError() = ERROR_ALREADY_EXISTS) then
begin
Application.MessageBox('该程序已在运行!','请注意……',
MB_OK + MB_ICONWARNING);
Exit;
end;
program project1;uses
Forms,
windows,
.....{$R *.RES}begin
CreateMutex(Nil,false,'project1');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
if Application.MessageBox('程序project1正在运行,是否再另外打开一个?','project1',4)=IDNO then
begin
SendMessage(HWND_BROADCAST,RegisterWindowMessage('project1'),0,0);
Halt(0);
end;
end;
Application.Initialize;
Application.CreateForm(Tfrmmain, frmmain);
Application.Run;
end.这样可以让用户自己决定是否再运行一次本程序。