Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

No Code - Just checking my facts

Posted on 2004-09-23
18
Medium Priority
?
301 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:gshep
  • 7
  • 6
  • 2
  • +3
18 Comments
 
LVL 23

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 300 total points
ID: 12131520
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
 
LVL 2

Expert Comment

by:__alex
ID: 12131607
Are you allowed to access a list view in a non VCL thread?
0
 

Author Comment

by:gshep
ID: 12131683
>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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:__alex
ID: 12131789
> 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
 

Author Comment

by:gshep
ID: 12131849
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
 
LVL 17

Expert Comment

by:geobul
ID: 12131955
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
 
LVL 2

Expert Comment

by:__alex
ID: 12132025
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
 

Author Comment

by:gshep
ID: 12132053
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
 

Author Comment

by:gshep
ID: 12132092
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
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12132122
Sort it in Critical Section...
0
 

Author Comment

by:gshep
ID: 12132141
>Sort it in Critical Section...

Even though the sort routine is in the main VCL thread?
0
 
LVL 2

Expert Comment

by:__alex
ID: 12132199
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
 
LVL 12

Assisted Solution

by:Ivanov_G
Ivanov_G earned 300 total points
ID: 12132203
the same way you access the ListView in other thread (critical section), it should be like this here ...
0
 
LVL 2

Accepted Solution

by:
__alex earned 1400 total points
ID: 12132222
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
 

Author Comment

by:gshep
ID: 12132322
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
 
LVL 7

Expert Comment

by:sftweng
ID: 12132335
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
 

Author Comment

by:gshep
ID: 12132355
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
 
LVL 2

Expert Comment

by:__alex
ID: 12132471
This is a good tutorial and it helped me a lot: http://www.pergolesi.demon.co.uk/prog/threads/ToC.html
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question