Click to See Complete Forum and Search --> : access UI elements from a thread


RCFox
February 16th, 2006, 02:56 PM
Hi,
The FAQ (below) is helpful, but I'm curious about something. Starting from this example, suppose the ThreadFunc gathers data (from network, serial port, etc.).

What is the best way to pass this data to an UI element (an editbox, for example) ?

Could a pointer to the data be passed in one of the PostMessage parameters (wParam or lParam) ?

Could someone alter part of this example to demonstrate how to allow data to be sent from the ThreadFunc to a dialog's editbox?

MFC Thread: How to access UI elements from a thread in MFC? (http://www.codeguru.com/forum/showthread.php?t=312454)

I appreciate your help.

gstercken
February 16th, 2006, 03:02 PM
What is the best way to pass this data to an UI element (an editbox, for example) ?

Could a pointer to the data be passed in one of the PostMessage parameters (wParam or lParam) ?Yes - but only if the data you are pointing to is guaranteed to have a sufficient lifetime to exist after the message has been posted. One possible way is to have the data managed by an object which has an equal or longer lifetime than the objects which retrieve and display the data - in that case, you could simply use a pointer as a member variable.
In any case, you need to make sure that the data is thread safe - therefore, you need to synchronize access to the data between threads (e.g. by using appropriate synchronization mechanisms like critical sections).

Marc G
February 16th, 2006, 03:07 PM
[ moved thread ]

Desade
February 16th, 2006, 03:35 PM
Starting from this example, suppose the ThreadFunc gathers data (from network, serial port, etc.).

The example uses arbitrary data in the form of a counter; however, this data could be anything you choose. Access to the data must just be synchronized as gstercken pointed out above.

Suppose that your thread is receiving a stock quote from the network in the form of a double price (100.50, etc.). A simplified approach to handling this scenario would be to have a variable to hold the price, and a critical section protecting access to it. Processing would follow these steps:

[ThreadFunc] Receives new price.
[ThreadFunc] Acquires critical section.
[ThreadFunc] Updates gfCurPrice.
[ThreadFunc] Releases critical section.
[ThreadFunc] Posts message WM_UPDATE_CONTROL a la the example.
[UI] Handles WM_UPDATE_CONTROL.
[UI] Acquires critical section.
[UI] Retrieves value of gfCurPrice.
[UI] Releases critical section.
[UI] Updates screen control attributes.

Depending on the amount and timing variability of the data you are receiving and displaying, a single critical section for a bunch of data elements may be the correct answer, or many critical sections -- one for each data element. For last-only information like stock quotes, this is an appropriate method (and if you are using an EditBox to display, you probably are dealing with last-only information).

RCFox
February 16th, 2006, 08:13 PM
Thank you, gstercken and Desade. This is very helpful for me.