OpenMutex 问题,紧急送分哦

LuckyGyt 2005-07-06 11:55:56
用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 了!


各位同仁,给点意见!
...全文
431 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
亮剑_ 2005-07-06
  • 打赏
  • 举报
回复
顶一下
beyondtkl 2005-07-06
  • 打赏
  • 举报
回复
你在進程關閉時 Release這個Mutex

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧