Recent

Author Topic: Can I get a TProcess to free itself when it's done processing?  (Read 1766 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Can I get a TProcess to free itself when it's done processing?
« on: February 19, 2018, 01:08:59 am »
I want to:

1) Create several processes that run independently, giving each a unique name
2) Store each one in an TObjectList
3) As the processes complete, I want them to free themselves and remove themselves from the TObjectList

This will allow me to do a search by name in the TObjectList to see if the are present.  If they are, they are still running.  If not present, the thing they were executing is finished.

How can I accomplish this?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Can I get a TProcess to free itself when it's done processing?
« Reply #1 on: February 19, 2018, 09:59:34 am »
Set TObjectList.OwnObjects property to true, pass its instance (pointer) to each TProcess descendant and let them hold it. At the end of Execute method, call TObjectList.Remove, that will remove the TProcess instance while also Free it. Note that Add and Remove call might need to be overridden to be thread safe (I believe it's not thread safe by default), but it depends on how you manage your TProcess instances.

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Can I get a TProcess to free itself when it's done processing?
« Reply #2 on: February 19, 2018, 03:18:58 pm »
First, why TObjectList instead of TStringList when you need a name?

2 Leledumbo: you don't need thread safety for TProcess unless threads involved (f.i. for poll purposes).

Start each process asynchronously (unset poWaitOnExit in Options).

To maintain list actuality, use polling "garbage collection" procedure f.i. using TTimer, check each TProcess.Running. If not, free process manually (or use OwnsObjects) and remove it from list.

If you don't need real-time usage (visualization?) of list, you don't need poll at all. Run "garbage collection" when you search/create new process.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: Can I get a TProcess to free itself when it's done processing?
« Reply #3 on: February 19, 2018, 03:36:20 pm »
Checking the .Running property seems to be the simplest.  No thread safety involved.

 

TinyPortal © 2005-2018