Inno Setup-防止同时执行多次安装程序 [英] Inno Setup - prevent executing the installer multiple times simultaneously

查看:366
本文介绍了Inno Setup-防止同时执行多次安装程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Inno Setup时遇到了麻烦:在用户计算机上,我的安装程序运行缓慢(我尚未诊断出的问题,可能是该计算机特有的问题,我仍然不知道).这导致该用户在第一个实例仍在执行的同时再次运行安装程序-令我惊讶的是,他们俩似乎都运行了一段时间,然后崩溃并烧毁...

I've got a bit of a pickle with Inno Setup: on a user machine, my installer was running slowly (something I've yet to diagnose, might be a problem specific with that computer, I still don't know). This lead to said user to run the installer again, while the first instance was still executing - and to my surprise, they both seemed to be running for a time, before crashing and burning...

我到处搜索,但是没有找到任何禁用此行为的方法-我的大多数查询都涉及Inno Setup互斥功能,这并不是我真正想要的.任何人都获得了有关如何确保安装程序仅执行一个实例/进程的提示?谢谢!

I searched around but have not found any way to disable this behavior - most of my queries wound up on Inno Setup mutex feature, which is not really what I'm looking for. Anyone got tips on how to make sure there is only one instance / process of the installer executing? Thank you!

推荐答案

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

Since Inno Setup 5.5.6 you can use the SetupMutex directive:

[Setup]
AppId=MyProgram
SetupMutex=SetupMutex{#SetupSetting("AppId")}

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

If you want to change a text of the message, that displays when another installer is running already, use:

[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.


在此版本之前,没有内置机制可用.但是您可以简单地编写自己的代码.原则是在安装开始时创建一个唯一的互斥锁.但是,首先,您要检查是否尚未创建这样的互斥体.如果是这样,则退出安装程序;如果不是,则创建互斥体:


Before this version, there was no built-in mechanism available. But you could write your own pretty simply. Principle is that you create a unique mutex when the setup starts. But, as first you check if there is no such mutex already created. If so, you exit the setup, if not, you create the mutex:

[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;

这篇关于Inno Setup-防止同时执行多次安装程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆