Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show progress during "PrepareToInstall"?

An installer I'm working on does most of its work in the PrepareToInstall function, as everything I need to do might fail, and therefore this is the appropriate place to handle these things in case they do fail. Any failure can be automatically reported by passing the error message in the function's result. There are only 3 small files which the installer actually copies over.

The problem is that the wizard seems to freeze (or not respond rather) during this function, just showing a blank page titled "Preparing to install..." while in reality, it's going through my install process.

I would like to show the progress to the user with a simple procedure ShowProgress(const S: String); which shows the user what it's actually doing. How can I do this?

This is how I'm doing my install, where I'd like to wrap each call to Log()...

function PrepareToInstall(var NeedsRestart: Boolean): String;
var
  R: Integer;
begin
  Result:= '';
  try
    Log('Doing this...');
    R:= DoThis;
    case R of
      0: begin
        Result:= '';
      end;
      1: begin
        Result:= 'Error message 1 was raised while doing this.';
      end;
      else begin
        Result:= 'Unexpected error doing this: ' + IntToStr(R);
      end;
    end;

    if Result = '' then begin
      Log ('Doing that...');
      R:= DoThat;
      case R of
        0: begin
          Result:= '';
        end;
        1: begin
          Result:= 'Error message 1 was raised while doing that.';
        end;
        else begin
          Result:= 'Unexpected error doing that: ' + IntToStr(R);
        end;
      end;
    end;

    if Result = '' then begin
      Log ('Doing something else...');
      R:= DoSomethingElse;
      case R of
        0: begin
          Result:= '';
        end;
        1: begin
          Result:= 'Error message 1 was raised while doing something else.';
        end;
        else begin
          Result:= 'Unexpected error doing something else: ' + IntToStr(R);
        end;
      end;
    end;

    //A few more blocks like above

    //Error logging
    if Result <> '' then begin
      Log('FAILURE: '+Result);
    end;

  except
    Result:= 'EXCEPTION';
    Log('EXCEPTION');
  end;
end;
like image 818
Jerry Dodge Avatar asked May 04 '14 17:05

Jerry Dodge


2 Answers

I mixed all your answers guys and I found another possible solution to this question.

I know this is not a so called 'elegant solution'. Anyway it is a quick solution that can avoid you to develop a separated DLL with custom controls managing the communication between the installer and the DLL.

The following approach describe how to create a custom Page that is a copy of the Page Inno Setup shows during PrepareToInstall setup phase. This copy must have the same look and feel of the original one plus a progress bar and a label that developer can use to indicate the current step of the PrepareToInstall setup phase.

Let's see some code.
First of all, prepare the custom Page:

[Code]
var
  PrepareToInstallWithProgressPage : TOutputProgressWizardPage;

Now define PrepareToInstall function to add our custom stuff:

function PrepareToInstall(var NeedsRestart: Boolean): String;
var
  ResultCode:   Integer;
begin
  PrepareToInstallWithProgressPage.SetProgress(0, 0);
  PrepareToInstallWithProgressPage.Show;

  try
    // First preinstallation step: suppose to kill currently app you are going to update 
    PrepareToInstallWithProgressPage.SetText('Exiting MyApp Running Instances'), '');

    ExecuteCmd('taskkill', '/F /IM MyApp');
    // Set progress bar to 10%
    PrepareToInstallWithProgressPage.SetProgress(1, 10);

    // Second preinstallation step
    // DoSomething usefull...
    // Set progress bar to 20% after done
    PrepareToInstallWithProgressPage.SetProgress(2, 10);

    // ...do other preinstallation steps till the end
    PrepareToInstallWithProgressPage.SetProgress(10, 10);
  finally
    PrepareToInstallWithProgressPage.Hide;
  end;
end;

At this installation phase we completed the steps for prepare to install phase so the default PrepareToInstall Page of Inno Setup is shown for a while.

Now, the user probably cannot understand that the page changes because our PrepareToInstallWithProgressPage has the same look and feel of the original one.

In order to let our page has the same look and feel we can use SetupMessage function to get the 2 strings of the original Inno Setup PrepareToInstall page.
Using SetupMessage we can avoid to duplicate and localize strings for our custom page to copy the originals.

Just like this:

procedure InitializeWizard;
var
  A: AnsiString;
  S: String;
begin
  // The string msgWizardPreparing has the macro '[name]' inside that I have to replace with the name of my app, stored in a define constant of my script.
  S := SetupMessage(msgPreparingDesc); 
  StringChange(S, '[name]', '{#MY_APPNAME}');
  A := S;
  PrepareToInstallWithProgressPage := CreateOutputProgressPage(SetupMessage(msgWizardPreparing), A);
end;

I hope this help.

like image 144
trix Avatar answered Oct 18 '22 19:10

trix


Have a look at the CodeDlg.iss example script included with Inno, in particular at the code that uses CreateOutputProgressPage.

This page type allows you to show a status message and/or a progress bar while you are performing other actions. And it automatically pumps messages whenever you change the progress label/value to ensure that the user sees the correct status.

It is intended for precisely this kind of code.

like image 26
Miral Avatar answered Oct 18 '22 18:10

Miral