CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    [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.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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)?

  3. #3
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    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.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Get value of a control in a property page in thread safe manner

    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.

    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.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    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.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  7. #7
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: Get value of a control in a property page in thread safe manner

    Thanks a lot. That does what I need.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured