Recent

Author Topic: Virtual treeview and background worker threads  (Read 655 times)

piola

  • Jr. Member
  • **
  • Posts: 53
Virtual treeview and background worker threads
« on: January 01, 2021, 06:50:46 am »
Dear all,

How can I properly "populate" a Virtual Treeview in a background thread? It is discouraged to use AddNode, so my question is specifically about OnGetText and OnInitChildren.

My idea is to start a background worker thread in the OnExpanded event. The worker would have both the treeview and the node saved and could easily add the correct nodes after it has finished ... but this is discouraged.

In the "virtual" paradigma, I assume I should use ReinitNode and ReinitChildren instead. This is also not a problem, both treeview and node are known to the worker thread. OnInitNode would then fire again.

But now I have a couple of problems:

#1: How do I detect that the node had been initialized before and there is not need to run a new worker thread?

#2: Finally, when OnGetText and OnInitChildren fire, I do not have any reference to the background worker containing the data that were populated before. There can, of course, be multiple worker threads if the user expanded multiple nodes at once.

So, how do I properly do it?

piola

  • Jr. Member
  • **
  • Posts: 53
Re: Virtual treeview and background worker threads
« Reply #1 on: January 16, 2021, 11:41:34 am »
Really no-one who could give me a hint?

GetMem

  • Hero Member
  • *****
  • Posts: 3884
Re: Virtual treeview and background worker threads
« Reply #2 on: January 16, 2021, 09:09:16 pm »
@piola
Quote
Really no-one who could give me a hint?
Perhaps because it's not exactly clear what are you trying to achieve. Anyways I created a demo application which will load a predefined directory into a VST. When the user double-click a folder, the containt of the folder is retrieved via a thread(see attached image). Hope it helps.

PS: The project was created with: Lazarus 2.0.10/FPC 3.2.0/VTV 5.5.3 
      If the user close the application, you must check if a thread is still running, this is not implemented.
      Feel free to ask more questions if you like.

piola

  • Jr. Member
  • **
  • Posts: 53
Re: Virtual treeview and background worker threads
« Reply #3 on: January 16, 2021, 09:52:12 pm »
Whew, great! Thank you very much. That's much more than I was hoping for. I'll try immediately and report back.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3977
  • I like bugs.
Re: Virtual treeview and background worker threads
« Reply #4 on: January 16, 2021, 10:01:09 pm »
How can I properly "populate" a Virtual Treeview in a background thread? It is discouraged to use AddNode, so my question is specifically about OnGetText and OnInitChildren.

My idea is to start a background worker thread in the OnExpanded event. The worker would have both the treeview and the node saved and could easily add the correct nodes after it has finished ... but this is discouraged.
Is OnInitChildren the event that returns the number of children? I think you should get it in the main thread. Then a worker thread can fill the expanded children using Synchronize().
Best would be to cache all needed data in advance so it is available for the events. Do you read from a DB? Is it really too slow?
I did not test GetMem's threaded solution yet. Maybe it has innovation I didn't realize.

Around 2007 I made an application with Delphi + VirtualTreeview using its proper virtual paradigm. There were 2 in-memory tables, Items and Relations, and a tree structure was constructed from them using indices on the fly.
Initial loading of data took a while but then it was lightning fast.
« Last Edit: January 16, 2021, 10:03:38 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

piola

  • Jr. Member
  • **
  • Posts: 53
Re: Virtual treeview and background worker threads
« Reply #5 on: January 16, 2021, 10:40:30 pm »
Okay, you are doing what I did with TreeView in former times and what is working well there: AddChild. However, the manual says about AddChild:

Quote
Using AddChild is not recommended. The method is merely there for easier migration from TTreeview. The reason is that the method has to validate the node and does some other processing, which prevents the tree from utilizings its virtual paradigm. Important advantages will so disappear. If possible you should restructure your design and try to use the right way: via OnInitNode

I have also used a VST before, so I'm not completely a newby. But I don't get the connections right. I have created an example, too. Maybe it can explain my problem a bit better. I took your thread, but wrote a new main unit. I have written a comment where my problem is.

Maybe I'm thinking too complicated?

piola

  • Jr. Member
  • **
  • Posts: 53
Re: Virtual treeview and background worker threads
« Reply #6 on: January 16, 2021, 10:43:49 pm »
Quote
Do you read from a DB? Is it really too slow?

The data have to be fetched from a network connection, so I want to avoid having lags in the UI.

GetMem

  • Hero Member
  • *****
  • Posts: 3884
Re: Virtual treeview and background worker threads
« Reply #7 on: January 16, 2021, 11:31:22 pm »
Okay, you are doing what I did with TreeView in former times and what is working well there: AddChild. However, the manual says about AddChild:

Quote
Using AddChild is not recommended. The method is merely there for easier migration from TTreeview. The reason is that the method has to validate the node and does some other processing, which prevents the tree from utilizings its virtual paradigm. Important advantages will so disappear. If possible you should restructure your design and try to use the right way: via OnInitNode

It's perferctly fine to use AddChild, it all depends on the requirements(you can do google search about this if you like). With Addchild I can add 1 million nodes in less then a second, do you really need a better performance then that? Did you experience any drawaback using AddChild?
Anyways if you wish to go with OnInitNode, OnInitChildren the same principles should apply.
« Last Edit: January 16, 2021, 11:42:06 pm by GetMem »

piola

  • Jr. Member
  • **
  • Posts: 53
Re: Virtual treeview and background worker threads
« Reply #8 on: January 17, 2021, 06:44:32 am »
Ok, I'll go the easier way and use AddChild then :D

Thanks for your help!

 

TinyPortal © 2005-2018