我在Inno安装程序中有点腌菜:在用户计算机上,我的安装程序运行缓慢(我还没有诊断的东西,可能是该计算机特有的问题,我仍然不知道)。这导致所述用户再次运行安装程序,而第一个实例仍在执行 - 令我惊讶的是,他们似乎都在运行一段时间,然后崩溃并烧毁......
我四处搜索但没有找到任何方法来禁用此行为 - 我的大部分疑问都在Inno Setup互斥功能上完成,这并不是我真正想要的。任何人都有关于如何确保安装程序只有一个实例/进程执行的提示?谢谢!
答案 0 :(得分:15)
自Inno Setup 5.5.6起,您可以使用SetupMutex
指令:
[Setup]
AppId=MyProgram
SetupMutex=SetupMutex{#SetupSetting("AppId")}
如果要更改已在另一个安装程序运行时显示的消息文本,请使用:
[Messages]
SetupAppRunningError=Setup has detected that %1 is currently running.%n%nPlease close all instances of it now, then click OK to continue, or Cancel to exit.
在此版本之前,没有可用的内置机制。但你可以简单地写自己的。原则是您在安装程序启动时创建一个唯一的互斥锁。但是,首先检查是否已经创建了这样的互斥锁。如果是,则退出设置,否则,创建互斥锁:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
const
// this needs to be system-wide unique name of the mutex (up to MAX_PATH long),
// there is a discussion on this topic http://stackoverflow.com/q/464253/960757
// you can expand here e.g. the AppId directive and add it some 'salt'
MySetupMutex = 'My Program Setup 2336BF63-DF20-445F-AAE6-70FD7E2CE1CF';
function InitializeSetup: Boolean;
begin
// allow the setup to run only if there is no thread owning our mutex (in other
// words it means, there's no other instance of this process running), so allow
// the setup if there is no such mutex (there is no other instance)
Result := not CheckForMutexes(MySetupMutex);
// if this is the only instance of the setup, create our mutex
if Result then
CreateMutex(MySetupMutex)
// otherwise tell the user the setup will exit
else
MsgBox('Another instance is running. Setup will exit.', mbError, MB_OK);
end;
答案 1 :(得分:-1)
如果您的安装程序名为setup.exe,那么您可以使用以下代码检查setup.exe是否正在运行并终止安装。
[Code]
function IsAppRunning(const FileName : string): Boolean;
var
FSWbemLocator: Variant;
FWMIService : Variant;
FWbemObjectSet: Variant;
begin
Result := false;
FSWbemLocator := CreateOleObject('WBEMScripting.SWBEMLocator');
FWMIService := FSWbemLocator.ConnectServer('', 'root\CIMV2', '', '');
FWbemObjectSet := FWMIService.ExecQuery(Format('SELECT Name FROM Win32_Process Where Name="%s"',[FileName]));
Result := (FWbemObjectSet.Count > 0);
FWbemObjectSet := Unassigned;
FWMIService := Unassigned;
FSWbemLocator := Unassigned;
end;
function InitializeSetup: boolean;
begin
result := not IsAppRunning('setup.exe');
if not result then
MsgBox('setup.exe is already running', mbError, MB_OK);
end;