[RESOLVED] Get value of a control in a property page in thread safe manner
Hi Guys,
I have a property page which has a list control on it. I need to know the number of items in the list control and read the text of the items from my main dialog box, so how can I do this in a thread safe manner? For adding, removing items to the list control I send user defined messages to the property page containing the list control. So for reading the items, should I send a message back to my main dialog? Or is there any cleaner/easier way of doing this?
Thanks.
Re: Get value of a control in a property page in thread safe manner
Is the property page in a different thread than the main dialog? Generally the answer to this is no unless you have created a second thread that displays the property page.
If you haven't explicitly created any threads, then you don't need to worry about thread safety (because both the dialog and the property page run in the same [UI] thread).
Okay threading issues aside, can you tell me if your property page is a modal (i.e. must be closed before accessing the main dialog) or non-modal (stays open along side the dialog)?
Re: Get value of a control in a property page in thread safe manner
Thanks for the response first of all.
Yes, the property page is created by the main thread. The reason I'm worried about thread safety issues is that I make updates to the list control from a separate thread. For adding/removing items I send a message to the property page which adds and removes items for me. But, I also need to know how many items are there in the list control? Which is the selected item? How to go about this?
Many Thanks.
Re: Get value of a control in a property page in thread safe manner
Quote:
The reason I'm worried about thread safety issues is that I make updates to the list control from a separate thread.
Repeat after me..."Windows Controls should only be updated from the Thread that created them, and this thread must be a UI thread (i.e. it has a message pump)"...Repeat again and again. :D
The proper design is for you to update some variable/structure with the desired information, post a message to the main thread (or use some other mechanism to indicate that an update is available), and then have the main thread update the actual control.
Any other implementation will eventually lead to difficulties, unreliable program operation, maintenance issues, etc.....
Re: Get value of a control in a property page in thread safe manner
Ofcourse, I know that. And, I update my controls by sending a message to the window that created them. But, the question over here is, from an another thread how I get the items selected in the list control, how I know how many items are there in the list control?
I hope I'm clear. Thanks.
Re: Get value of a control in a property page in thread safe manner
Okay, let's say you've created a CDialog class with a static thread proc. In the thread proc, you pass in the pointer of the class, so you can access the CDialog class members within the class. You still can't access any UI members but you can access non-UI methods/fields.
Now that we have this structure, in the main UI thread, you need to have two long variables (m_lListCount, m_lSelCount) that tracks the total items and selected items. Whenever the list count changes or the selected item count changes you must update these variables.
Since these variables need to be accessed by both threads, you need to synchronize access to them. While you could use a critical section, it's easier to use the InterlockedXXX family of functions in this case.
So all you need to do is create a pair of setters/getters for each variable and ALWAYS calls these to access the variable. NOTE: from this point on, in order for the variable to be protected, you must never access it directly - only through the get/set accessor methods.
Declare the following within the class of the CMyDialog.h file (I'm only showing the list count methods, you'll need equivalent for the selected count).
Code:
long GetListCount( )
{
return InterlockedExchange( &m_lListCount, m_lListCount );
}
void SetListCount( long lCount )
{
InterlockedExchange( &m_lListCount, lCount );
}
As mentioned anytime the list changes in the main UI thread, just call the SetListCount method and update the count. Conversely, the secondary thread will call GetListCount to retrieve the count in a threadsafe manner.
Re: Get value of a control in a property page in thread safe manner
Thanks a lot. That does what I need.