Forum > Beginners

Make main class wait for another class (console program)

(1/4) > >>

BlueMoony:
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:

--- Code: ---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;
--- End code ---

Main function itself (was auto generated)

--- Code: ---var
  Application: Main;
begin
  Application:=Main.Create(nil);
  Application.Title:='Main';
  Application.Run;
  Application.Free;
end.                     
--- End code ---

Code for executing the controller:

--- Code: ---procedure TController.Execute();
var
  ZoneSetting: TZoneSettings;
  i: integer;
begin
      // Do other stuff (fill in array)
      CheckUpdate();
      ShowMessage('Updating enabled');
      Application.ProcessMessages;
end;
--- End code ---

--- Code: ---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;
--- End code ---

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

Blaazen:
Can you explain one thing, you say "console application" and then you talk about forms.


--- Quote ---Question: how to make the main class wait for the other class?
--- End quote ---
In general, it's possible with events. When subclass ends, it triggers an event.

BlueMoony:

--- Quote from: Blaazen on August 11, 2014, 10:16:17 pm ---Can you explain one thing, you say "console application" and then you talk about forms.


--- Quote ---Question: how to make the main class wait for the other class?
--- End quote ---
In general, it's possible with events. When subclass ends, it triggers an event.

--- End quote ---

And with subclass ends, you mean when it destroys itself?

Blaazen:
Yes, similarly to OnDestroy event of form.

BlueMoony:
The main unit is a console application and so is the controller unit.
The controller unit has an array of other  units which are forms.
I want the forms to stay up all the time but since the execute procedure ends, so will the main function executeController(). This means the main unit will call the Terminate and Exit functions. This is not what I want since I started a TTimer in the Controller unit which will do stuff.

So how can I keep the controller unit and main unit "running"?

Navigation

[0] Message Index

[#] Next page

Go to full version