1 . 增加引入單元 :
use System,Windows;
2. 定義互斥句柄變量:
var
MutexHandle: Thandle;
3. 建立互斥實例, 並以此作為服務程序是否運行的判斷依據:
begin
MutexHandle := CreateMutex(nil,True, 'Main');
if MutexHandle <> 0 then
begin
if GetLastError = ERROR_ALREADY_EXISTS then
begin
CloseHandle(MutexHandle);
Halt;
end;
end;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.