'Check' function is executing multiple times in Inno Setup

人盡茶涼 提交于 2020-04-08 07:22:00

问题


I am new to Inno Setup scripting and I am trying to install .NET framework 3.5 using below code as a prerequisite. The Check function is executing multiple times. Can some one please help me understand why?

Note: All other sections (Setup, Icons, etc) in this below code are having proper contents.

[Files]
Source: "Frameworks\dotnetfx35setup.exe"; DestDir: {tmp}; Flags: deleteafterinstall; \
    BeforeInstall: Install35Framework; Check: Framework35IsNotInstalled
[Code]
function IsDotNetDetected(version: string; service: Cardinal): boolean;
begin
  Result := { ... };
end;

function Framework35IsNotInstalled: Boolean;
begin
  if IsDotNetDetected('v3.5', 1) then
  begin
    MsgBox('Framework35IsNotInstalled: FALSE ', mbConfirmation, MB_YESNO);
    Result := False;
  end else begin
    MsgBox('Framework35IsNotInstalled: TRUE ', mbConfirmation, MB_YESNO);
    Result := True;
  end;
end; 

procedure Install35Framework;
begin
  { ... }
end;

回答1:


Quoting Check parameter documentation:

Setup might call each check function several times, even if there's only one entry that uses the check function. If your function performs a lengthy piece of code, you can optimize it by performing the code only once and 'caching' the result in a global variable.

So the behavior is as designed.

And as your code is quite simple, I do not even think it needs any optimization. It's perfectly ok, if it runs few times.


Were it not, you can optimize it like this:

var
  Framework35IsNotInstalledCalled: Boolean; 
  Framework35IsNotInstalledResult: Boolean;

function Framework35IsNotInstalled: Boolean;
begin
  if not Framework35IsNotInstalledCalled then
  begin
    Framework35IsNotInstalledResult := IsDotNetDetected('v3.5', 1);
    Framework35IsNotInstalledCalled := True;
  end;

  Result := Framework35IsNotInstalledResult;
end; 


来源:https://stackoverflow.com/questions/46037128/check-function-is-executing-multiple-times-in-inno-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!