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.
gshepAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
__alexConnect With a Mentor Commented:
The synchronize method sends a message and waits (thread is suspended) until the message is processed. A click on a control sends a message. The first message is served first (with exceptions: WM_PAINT, ...).
0
 
Ferruccio AccalaiConnect With a Mentor Senior developer, analyst and customer assistance Commented:
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...
I'm not sure about the priority, but i guess that the answer could be no, as i guess that any thread calls a beginupdate/endupdate...
In my opinion you could do the sort in the critical section too to be sure that during it  threads wait...

F68 ;-)
0
 
__alexCommented:
Are you allowed to access a list view in a non VCL thread?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gshepAuthor Commented:
>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?
0
 
__alexCommented:
> 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.
0
 
gshepAuthor Commented:
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.
0
 
geobulCommented:
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
0
 
__alexCommented:
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!
0
 
gshepAuthor Commented:
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.
0
 
gshepAuthor Commented:
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?
0
 
Ivanov_GCommented:
Sort it in Critical Section...
0
 
gshepAuthor Commented:
>Sort it in Critical Section...

Even though the sort routine is in the main VCL thread?
0
 
__alexCommented:
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
0
 
Ivanov_GConnect With a Mentor Commented:
the same way you access the ListView in other thread (critical section), it should be like this here ...
0
 
gshepAuthor Commented:
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
0
 
sftwengCommented:
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.
0
 
gshepAuthor Commented:
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.
0
 
__alexCommented:
This is a good tutorial and it helped me a lot: http://www.pergolesi.demon.co.uk/prog/threads/ToC.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.