OpenMutex 问题,紧急送分哦
用CreateMutex 保证应用程序只启动一个实例
program Project1;
function CheckPreviousInstance:boolean;
begin
Result :=False;
CreateMutex(nil, True, 'test');
Result := GetLastError = ERROR_ALREADY_EXISTS;
end;
begin
if not CheckPreviousInstance then
begin
Application.Initialize;
Application.Title :='';
Application.CreateForm(TForm2, Form2);
Application.Run;
end;
end.
在另外程序中 进行检测判断上面Project1.exe 是否存在,
如果存在什么也不做,不存在则启动Project1.exe
procedure TForm1.Button3Click(Sender: TObject);
var
aHandle :Hwnd;
begin
try
begin
aHandle := OpenMutex(MUTEX_ALL_ACCESS,False,PChar('test'));
if aHandle<>0 then
begin
case WaitForSingleObject(aHandle,500) of
WAIT_OBJECT_0:
begin
ReleaseMutex(aHandle);
end;
WAIT_ABANDONED:
begin
// ReleaseMutex(aHandle);
WinExec(Pchar('c:\'+'project1.exe'),SW_HIDE);
end;
end;
end else WinExec(Pchar('c:\'+'project1.exe'),SW_HIDE);
//
end;
// if not ExistMutex(Test) then WinExec(Pchar('c:\'+'project1.exe'),SW_HIDE);
except
end;
end;
这个程序有点问题:在Project1.exe 存在时,多次执行检测程序。在关闭project1.exe后
执行检测程序,就无法启动Project1.exe 了!
各位同仁,给点意见!