Welcome to the new DelphiDabbler Code Library Documentation.

This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.

Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.

Console Application Runner Classes Example 3: Indicating progress

Applies to: ~>3.0

It is common to want to display some kind of progress indicator while another application is running. This example shows how to do that using TPJConsoleApp. Our application needs to handle the OnWork event to get notified whenever the console application yields control in order to update a progress bar.

Start a new Delphi GUI application, drop a progress bar and a button on the form. Set the progress bar’s Max property to 10. As in Example 2 we’re going to need an OnWork event handler, so create the private method:

procedure WorkHandler(Sender: TObject);

This time implement the method as follows:

procedure TForm1.WorkHandler(Sender: TObject);
begin
  if ProgressBar1.Position = ProgressBar1.Max then
    ProgressBar1.Position := 0
  else
    ProgressBar1.Position := ProgressBar1.Position + 1;
  Application.ProcessMessages;
end;

Now create an OnClick event handler for the button as follows:

procedure TForm1.Button1Click(Sender: TObject);
var
  App: TPJConsoleApp;
begin
  App := TPJConsoleApp.Create;
  try
    App.MaxExecTime := INFINITE;  // don't time out
    App.TimeSlice := 100;         // yield to main app every 1/10 second
    App.Visible := True;          // ensure we see the app
    App.OnWork := WorkHandler;    // assign the event handler
    if not App.Execute('Timed 5') then  // run Timed.exe for 5 seconds
      ShowMessage('Failed to run Timed.exe');
  finally
    App.Free;
  end;
end;

This is the same code we used in Example 2 - all the changes are in the OnWork event handler.

We’re using Timed.exe from Appendix 2 again - change it if you wish.

Click the button to run the console application, switch back to the main form and watch the progress bar.