Solved

No Code - Just checking my facts

Posted on 2004-09-23
18
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
  • +3
18 Comments
 
LVL 22

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 75 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 75 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 350 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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