只允许运行一个程序
避免程序运行两个,一般后台服务都需要这个功能,工程文件修改事例如下:
program printService;
uses
Forms,
u_main in 'u_main.pas' {Form1};
{$R *.res}
var //避免程序的二次运行;
// h: HWND;
hMutex: THandle;
begin
hMutex := OpenMutex(MUTEX_ALL_ACCESS, True, 'printService');
if hMutex <> 0 then
begin
showmessage('后台程序已经在运行,请看有下角的图标!');
// MessageBox(handle, '后台程序已经在运行,请看有下角的图标!', '温馨提示', MB_OK
// + MB_ICONINFORMATION);
Exit;
end;
hMutex := CreateMutex(nil, False, 'printService');
if hMutex = 0 then
begin
Exit;
end;
try
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
finally
ReleaseMutex(hMutex);
end;
end.
浙公网安备 33010602011771号