Inno Setup - 防止同时多次执行安装程序

Tak*_*shi 9 inno-setup

我对Inno安装程序有点挑剔:在用户机器上,我的安装程序运行缓慢(我还没有诊断的东西,可能是该计算机特有的问题,我仍然不知道).这导致所述用户再次运行安装程序,而第一个实例仍在执行 - 令我惊讶的是,他们似乎都在运行一段时间,然后崩溃和刻录......

我四处搜索但没有找到任何方法来禁用此行为 - 我的大部分疑问都在Inno Setup互斥功能上完成,这不是我想要的.任何人都有关于如何确保安装程序只有一个实例/进程执行的提示?谢谢!

TLa*_*ama 15

自Inno Setup 5.5.6起,您可以使用该SetupMutex指令:

[Setup]
AppId=MyProgram
SetupMutex=SetupMutex{#SetupSetting("AppId")}
Run Code Online (Sandbox Code Playgroud)

如果要更改已在另一个安装程序运行时显示的消息文本,请使用:

[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.
Run Code Online (Sandbox Code Playgroud)

在此版本之前,没有可用的内置机制.但你可以简单地写自己的.原则是您在安装程序启动时创建一个唯一的互斥锁.但是,首先检查是否已经创建了这样的互斥锁.如果是,则退出设置,否则,创建互斥锁:

[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;
Run Code Online (Sandbox Code Playgroud)