hMutex := CreateMutex(nil, False, 'SSS'); Ret := GetLastError; if Ret <> ERROR_ALREADY_EXISTS then begin Application.CreateForm(TFmMain, FmMain); Application.CreateForm(TFmAbout, FmAbout); Application.Run; end else Application.Terminate; ReleaseMutex(hMutex);
hMutex0 := CreateMutex(nil, true, 'model'); Ret := GetLastError; if Ret <> ERROR_ALREADY_EXISTS then begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end else begin if Application.MessageBox('程序已经运行,是否退出?', '提示!', MB_OKCANCEL or MB_ICONWARNING or MB_DEFBUTTON2)=2 then exit; Application.Terminate; ReleaseMutex(hMutex0); end;我只是加了一个对话框,我试过去掉对话框提示也一样,程序还是无法退出,没有任何反应
检查下你的代码逻辑 if Ret <> ERROR_ALREADY_EXISTS then begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end else begin if Application.MessageBox('程序已经运行,是否退出?', '提示!', MB_OKCANCEL or MB_ICONWARNING)=IDOK then Application.Terminate else begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end; end; ReleaseMutex(hMutex0);
hMutex := CreateMutex(nil, False, 'SSS');
Ret := GetLastError;
if Ret <> ERROR_ALREADY_EXISTS then
begin
Application.CreateForm(TFmMain, FmMain);
Application.CreateForm(TFmAbout, FmAbout);
Application.Run;
end
else
Application.Terminate;
ReleaseMutex(hMutex);
Ret := GetLastError;
if Ret <> ERROR_ALREADY_EXISTS then
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else
begin
if Application.MessageBox('程序已经运行,是否退出?', '提示!', MB_OKCANCEL or MB_ICONWARNING or MB_DEFBUTTON2)=2 then exit;
Application.Terminate;
ReleaseMutex(hMutex0);
end;我只是加了一个对话框,我试过去掉对话框提示也一样,程序还是无法退出,没有任何反应
if Ret <> ERROR_ALREADY_EXISTS then
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else
begin
if Application.MessageBox('程序已经运行,是否退出?', '提示!', MB_OKCANCEL or MB_ICONWARNING)=IDOK then
Application.Terminate
else
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
end; ReleaseMutex(hMutex0);
如果这样都不行,那么就来个狠得:var
FPid: DWORD;
begin
GetWindowThreadProcessId(Application.Handle, FPid);
WinExec(PAnsiChar('TASKKILL.EXE /F /PID ' + IntToStr(FPid)), 0)
end;
PS:为什么要关闭第一个实例呢?难不成你第二个实例和第一个不一样????