Recent

Author Topic: [solved] how to control the order of creation.  (Read 855 times)

mas steindorff

  • Hero Member
  • *****
  • Posts: 553
[solved] how to control the order of creation.
« on: December 10, 2024, 02:26:16 am »
I have several programs that use lots of classes (libraries) that have been created in each module's initialization section.  this approach has worked for years and creates system classes that track and analyze different subsystems.
I do have a core class that must be created 1st and then the others did not have any dependencies on each other so their order did not mater unit now..

I would like to register some call back functions across these classes.  if I do it during the create, there seem to be issues I call the chicken and the egg.
I tried using .Aftercreation() but the stack shows it is being called directly after the end of the .create(), not waiting for the rest of the code to initialize before being called. 
These are libraries and do not have a "main" form.

1: without resorting to a timer, is there a function that is called after everything is created?

2:or is there a way to add these functions to the application process messages so they are delayed until it is fully started?

3: is there a better approach?
   
thanks MAS
« Last Edit: December 11, 2024, 02:15:02 am by mas steindorff »
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

TRon

  • Hero Member
  • *****
  • Posts: 3787
Re: how to control the order of creation.
« Reply #1 on: December 10, 2024, 05:00:02 am »
1: without resorting to a timer, is there a function that is called after everything is created?
I do not know what exactly you indicate with "everything" but assuming you meant after all initialization sections have been processed then that would be the main program entry (main).

Quote
2:or is there a way to add these functions to the application process messages so they are delayed until it is fully started?
In the context of your description I did not fully comprehend what you seem to imply here. Sorry for that.

Quote
3: is there a better approach?
Depends on the situation. One scenario might be to not use the initialization sections but instead use f.e. a module to create your classes (in any which order the code requires it to do), use that module as your "first form" that gets created before anything else and create the other forms after that (before running your application). The same thing can be achieved by a custom class that initializes all other dependencies first.

In case you really have to rely on unit initialization then instead of creating the class at initialization, add the class names or init routines to a (ordered) list and let that list take care of things.

Other scenarios are possible, including fiddling with setjmp (but that usually screw with my brain, in the unlikely case I might actually have one  :) ).
   
In the end it depends on the use case and what problem (if any) you are trying to solve.
I do not have to remember anything anymore thanks to total-recall.

cdbc

  • Hero Member
  • *****
  • Posts: 1771
    • http://www.cdbc.dk
Re: how to control the order of creation.
« Reply #2 on: December 10, 2024, 08:44:16 am »
Hi
In my App 'MVP-Setup', I'm doing something similar, i.e.: registering stuff that should happen later... You can find it here.
In that I use a queue to store names until the model comes up and can use them ...but you can just as easily, store function pointers in the queue  8)
Have a looksee in 'model.intf', 'model.main->create' & ind the initialization sections of the units.
HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1771
    • http://www.cdbc.dk
Re: how to control the order of creation.
« Reply #3 on: December 10, 2024, 12:15:39 pm »
Hi
Sorry, just checked, I haven't updated gitlab yet... Silly me  :P
On the other hand there's a zipped project to download in reply #29 in this thread.
Sorry for the incenvenience... :-[
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

mas steindorff

  • Hero Member
  • *****
  • Posts: 553
Re: how to control the order of creation.
« Reply #4 on: December 10, 2024, 08:47:04 pm »
thank you both.
cdbc response had me realize I did have a common "communication" object that must be started first.   this part works.  it even has a timer and does not need any of my other objects/classes.
 
I'll use your approach and add a generic queue to this object. I'll use the timer to process my new queue of procedures and act like an "AfterStart()" function that will let my other objects interact with each other as needed.

This will work like my #2 above where I was asking if I could add a  function to the application queue <where other On(action) function are queued when triggered>.  I know it exist but I do not know how to work with it directly.

Thanks again. MAS
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

 

TinyPortal © 2005-2018