Inno Setup - prevent executing the installer multiple times simultaneously

前端 未结 2 1368
走了就别回头了
走了就别回头了 2020-12-16 01:17

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 compu

2条回答
  •  一向
    一向 (楼主)
    2020-12-16 01:42

    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;
    

提交回复
热议问题