Link to home
Start Free TrialLog in
Avatar of gshep
gshep

asked on

No Code - Just checking my facts

I have a ListView component (in report style) that is updated by nearly 500 threads (the program manages some 500 files and updates their status such as who has the file open, etc in the Listview).  Each thread enters a critical section to update the ListView so that only one thread at a time is writing to the ListView - that all works as it is supposed to (so far).

However, what i want to now do is put a routine in the main program that will sort the coulmns when the user clicks on the column heading.

What i am wondering is, is it safe to sort the ListView while the threads are updating information on it?  Will the main program get priority over any thread trying to update/insert data?

Or should i make it that while the column is being sorted, all other threads must wait?

I don't need code...just some advice.
SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of __alex
__alex

Are you allowed to access a list view in a non VCL thread?
Avatar of gshep

ASKER

>Are you allowed to access a list view in a non VCL thread?

Yes, to update it using Synchronize method.  But since the user can click on the ListView anytime i wonder if data corruption might occur.

>As the sort is done comparing items, i think that a sort during a threaded update/insert could return some problems like some data mixing.

Data corruption is the one thing i was thinking might happen.  What if i put critical sections around the actual ListView code - ie: inside the component itself?  Good idea or bad?
> Yes, to update it using Synchronize method.
That means you switch from your thread to the VCL thread! Unless you do not have an Application.ProcessMessages() in your synchronized code no other event (mouse click, ...) can 'interrupt' it.
Avatar of gshep

ASKER

yes, all the "worker" threads update the listview via the Synchronize method - but i am wondering if information is being updated, deleted, etc by the threads and the user clicks on the ListView column to sort it, would there not be a possibility of a clash of two threads?  ie:a worker thread is in the middle of updating a row of information in the ListView and the user clicks the column heading to sort it - does the main VCL thread then wait till the worker thread is finished or does it try to sort the column ignoring the worker thread which is in the middle of an update?    If that was the case, then i would think there would be data corruption just waiting to happen.
Hi,

What are you using for updating the ListView - critical sections or Synchronize? You mentioned both. This is important to be made clear before answering.

Regards, Geo
Time goes down in this table:

Worker thread                                 VCL thread                        You
-------------------------------------------------------------------------------------------------------
Observe file
Calculate data
Synchronize(UpdateView)   ---->      Execute UpdateView
Sleep                                                      "
Sleep                                                      "                               Press button
Sleep                                                      "
Go ahead                          <----      UpdateView done
   "                                                 Look for events (*)
                                                     Find a WM_BUTTON_WHATEVER
                                                     Execute list view sorting

(*) Application.Run in the DPR file will call Application.ProcessMessages for you until you close your app.

In other words: You don't execute any list view related code in a worker thread. You even don't need to protect your view with a critical section!
Avatar of gshep

ASKER

Sorry, i use critical sections for parts of the worker thread that don't operate on the interface - I use Synchronize in each worker thread when they need to update the interface (main VCL).  Some of the Synchronize actions are within critical sections too.
Avatar of gshep

ASKER

Alex - what about...

Worker thread                                 VCL thread                        You
-------------------------------------------------------------------------------------------------------
Observe file
Calculate data
Synchronize(UpdateView)   ---->      Execute UpdateView        Press button <X>
Sleep                                                      "
Sleep                                                      "                              
Sleep                                                      "
Go ahead                          <----      UpdateView done
  "                                               


At <X>, since i clicked on the listview column to sort it and it has already begun updating inforfation from Synchronize(UpdateView)  in the worker thread what are the event?  Does the VCL ignore the button click/column header click until it has finished with UpdateView - or does it try to do both at the same time, etc?
Sort it in Critical Section...
Avatar of gshep

ASKER

>Sort it in Critical Section...

Even though the sort routine is in the main VCL thread?
Worker thread                                 VCL thread                        You
-------------------------------------------------------------------------------------------------------
Observe file
Calculate data                                                                        Press button
        "                                           Execute list view sorting
Synchronize(UpdateView)                          "
Sleep                                            List view sorting done
Sleep                                            Look for events (*)
Sleep                                            Find a synchronize message
Sleep                                            Execute UpdateView
Go ahead                          <----      UpdateView done
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gshep

ASKER

ok, thanks to everyone for their input.   It all gives me something to work on.

I raised the points and split them among the three main contributors
Have you considered whether use of the TMultiReadExclusiveWrite synchronizer could be appropriate? You could use exclusive write access to a common structure (non-vcl) in the threads and multi read access from the main (Synchronize) thread to transfer the data (sorted) into the VCL component.
Avatar of gshep

ASKER

sftweng - most of the program is completed.  It took a while for me to understand critical sections enough to use them let alone a TMultiReadExclusiveWrite synchronizer lol.  But thanks for the info.
This is a good tutorial and it helped me a lot: http://www.pergolesi.demon.co.uk/prog/threads/ToC.html