代码如下
var
hMutex: DWORD;
Ret: Integer;
{以上代码要放在begin..end上}
hMutex := 0;
try
hMutex := CreateMutex(nil, False, 'eShapeOfClient'); //TRUE 标明该进程拥有此 Mutex 对象eShapeOfClient为自己的程序文件名或自己定义的任意名称
Ret := GetLastError;
if (Ret = ERROR_ALREADY_EXISTS) then //Mutex 对象是否存在
begin
ReleaseMutex(hMutex);
Application.Terminate; //退出程序
end
else if hMutex = ERROR_INVALID_HANDLE then
begin
InfoDlg('此程序正在运行,关闭后请重新再试!'); //InfoDlg函数是自己定义的,通常用MessgeBox来代替
Application.Terminate;;
end;
finally
ReleaseMutex(hMutex);
end;
hMutex: DWORD;
Ret: Integer;
{以上代码要放在begin..end上}
hMutex := 0;
try
hMutex := CreateMutex(nil, False, 'eShapeOfClient'); //TRUE 标明该进程拥有此 Mutex 对象eShapeOfClient为自己的程序文件名或自己定义的任意名称
Ret := GetLastError;
if (Ret = ERROR_ALREADY_EXISTS) then //Mutex 对象是否存在
begin
ReleaseMutex(hMutex);
Application.Terminate; //退出程序
end
else if hMutex = ERROR_INVALID_HANDLE then
begin
InfoDlg('此程序正在运行,关闭后请重新再试!'); //InfoDlg函数是自己定义的,通常用MessgeBox来代替
Application.Terminate;;
end;
finally
ReleaseMutex(hMutex);
end;
以上代码没有任何问题,但在运行第二个程序关闭时,也就是Application.Terminate; 会有屏闪,如果改成Exit;好像没有问题。