'Check' function is executing multiple tim

2020-06-06 02:44发布

问题:

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;