C++ sendng messages to GUI controls from different threads (thread - safety)
Posted on 2011-04-26
I would like to set my mind straight on this once and for all. When calling GUI controls from other threads, I know you have to send messages. Can thIs be the case for non-GUI objects?
As an example to help answer this question, I'm writing a progress bar control that I want to have the method "SetCurrentProgress(CString strText, double dPercentProgress)". I primarily want to know how to make this function thread-safe, and easily accessible to my whole application (I guess making it global is the easiest/best way for easy access?)
My usual approach is to have a SendMessage call within the SetCurrentProgress function so that the message is sent to the window of the progress bar and ends up being on the GUI thread instead of whatever the calling thread is on. But my progress bar doesn't update. So I try doing MessageQueueHelper::DoEvents to force the events in the event loop to occur, and this seems problematic as well..... I've had this kind of thing more or less working nicely before, but I really need to know the best way, and understand what's going wrong exactly when things don't seem to work.
Thanks for your help!