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.
Applies to: ~>3.0
As noted in Example 1, it’s often worth letting a GUI program remain interactive - may be to display a progress bar or whatever.
To do this you need to handle TPJConsoleApp’s OnWork event.
Start a new Delphi GUI application and, in the form’s private section, insert the following new method declaration:
procedure WorkHandler(Sender: TObject);
Implement the method as follows:
procedure TForm1.WorkHandler(Sender: TObject);
begin
Application.ProcessMessages;
end;
Now drop a TButton and add the following code as its OnClick event handler:
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;
The main code here is similar to Example 1 except that the TimeSlice property is now set to 100
rather than INFINITE
and we have assigned a handler for the OnWork event. Setting TimeSlice forces the console app to yield to the GUI app every 1/10th of a second. When this happens TPJConsoleApp triggers the OnWork event, and our event handler lets the GUI application receive messages.
Run the program. Try to switch back to the GUI while the console app is running. Unlike in Example 1, you can now do it!
We’ve used
Timed.exe
from Appendix 2 once again. Substitute another suitable program if you wish, providing it runs for a significant amount of time.