用互斥(Mutex),Mutex本来是多线程同步用的,防止多个线程访问同一资源而引发冲突。用CreateMutex可以创建Mutex对象,实质上是一个kernel对象。program Project1;uses Forms, windows, Dialogs, Unit1 in 'Unit1.pas' {Form1};{$R *.res} var MutexHandle:Thandle; begin Mutexhandle:=windows.CreateMutex(nil,true,'能代表程序特点的字符串'); if Mutexhandle<>0 then begin if getlasterror=windows.ERROR_ALREADY_EXISTS then begin ShowMessage('已有一实例在运行了'); closehandle(Mutexhandle); halt; end;end;
Forms,
windows,
Dialogs,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}
var MutexHandle:Thandle;
begin
Mutexhandle:=windows.CreateMutex(nil,true,'能代表程序特点的字符串');
if Mutexhandle<>0 then
begin
if getlasterror=windows.ERROR_ALREADY_EXISTS then
begin
ShowMessage('已有一实例在运行了');
closehandle(Mutexhandle);
halt;
end;end;