Dear community,
don't really know this question resembles in this beginners section, but it looks like a beginners question to me.
What I would like to do:
1) Run console application which creates an additional class depending on its arguments
2) If this class is the controlling class, it should setup a few other classes and some timers. This class should not be freed nor should the program stop or exit.
Question: how to make the main class wait for the other class?
Some code I use:
DoRun function of main console application:
procedure Main.DoRun;
var
ErrorMsg: String;
begin
// quick check parameters
// if wrong parameter -> exception
{ErrorMsg:=CheckOptions('h','help');
if ErrorMsg<>'' then begin
ShowException(Exception.Create(ErrorMsg));
Terminate;
Exit;
end;}
// parse parameters
if HasOption('h','help') then begin
WriteHelp;
Terminate;
Exit;
end;
// Create settings object
_appSettings:= TSettings.Create();
// Check #parameters
if(ParamCount <= 2) then
begin
// Amount of parameters is 1 or 2 (debug)
// Running instance will be a controller
_appSettings.AppType:= 'controller';
ExecuteController();
end
else
begin
// More parameters = specific part
// First fill in parameters
_appSettings.AppType:= ParamStr(1);
_appSettings.ZoneName:= ParamStr(2);
_appSettings.Left:= StrToInt(ParamStr(3));
_appSettings.Top:= StrToInt(ParamStr(4));
_appSettings.Width:= StrToInt(ParamStr(5));
_appSettings.Height:= StrToInt(ParamStr(6));
_appSettings.ContentPath:= ParamStr(7);;
// Next is check type & launch
case _appSettings.AppType of
'image':
begin
ExecuteImage();
end;
'banner':
begin
ExecuteBanner();
end;
'html':
begin
ExecuteHtml();
end;
end;
end;
ShowMessage('End of loop');
// stop program loop
Terminate;
Exit;
end;
Main function itself (was auto generated)
var
Application: Main;
begin
Application:=Main.Create(nil);
Application.Title:='Main';
Application.Run;
Application.Free;
end.
Code for executing the controller:
procedure TController.Execute();
var
ZoneSetting: TZoneSettings;
i: integer;
begin
// Do other stuff (fill in array)
CheckUpdate();
ShowMessage('Updating enabled');
Application.ProcessMessages;
end;
function TController.CheckUpdate():integer;
begin
try
begin
_updateTimer := TTimer.Create(nil);
with _updateTimer do
begin
Interval := 1000;
Enabled := True;
OnTimer := @OnTimerTick;
end;
end;
except
end;
end;
//______________________________________________________________________________
//
procedure TController.OnTimerTick(Sender: TObject);
begin
ShowMessage('Timer ticked!');
end;
What happens:
1) Controller starts fine and fills in the array and creates other classes
2) It starts the timer and the timer runs fine (every second) until I hit the 'OK' on the 'Updating enabled' message. After that, the application quits.
How am I able to keep this all running? The other classes display several forms on the screen which need to be there all the time. The update function will change these forms at certain intervals through the array I filled in before. However, the application closes.
Methods I already tried:
1) Application.Idle in the controller
2) Application.Idle in the Main unit
3) While true do begin sleep(10000) end -> program hangs and so does the timer (no ticks)
Any suggestions? (like in how does a form wait for any event to occur? That object should somehow use some kind of loop no?)
Additional info:
Lazarus version 1.0.12
FPC: 2.6.2
x86_64-linux-gtk_2