Updating UI from thread
-
I have a worker thread for example, DWORD ThreadProc(void* pVal) { while() { pcEdit->SetWindowText("Status"); pcEdit2->SetWindowText("Status2"); } } Is it okay to update UI this way from the thread? Or how should I do? Anybody can explain in simple words? The above actually works fine. But mate says I shouldn't do it, but he doesn't say how to do it. :doh:
---------------------------- 286? WOWW!:-O
-
I have a worker thread for example, DWORD ThreadProc(void* pVal) { while() { pcEdit->SetWindowText("Status"); pcEdit2->SetWindowText("Status2"); } } Is it okay to update UI this way from the thread? Or how should I do? Anybody can explain in simple words? The above actually works fine. But mate says I shouldn't do it, but he doesn't say how to do it. :doh:
---------------------------- 286? WOWW!:-O
The right way to do it is to post messages to the thread that owns the UI. Use the PostMessage[^] API to post messages. For
SetWindowText
post the WM_SETTEXT[^] message. The problem that can occur is such scenarios is when both the worker thread and the thread that owns the UI simultaneously updates the control. ForSetWindowText
, this may not be a problem because the function does the same thing of sending theWM_SETTEXT
message.«_Superman_» I love work. It gives me something to do between weekends.
-
The right way to do it is to post messages to the thread that owns the UI. Use the PostMessage[^] API to post messages. For
SetWindowText
post the WM_SETTEXT[^] message. The problem that can occur is such scenarios is when both the worker thread and the thread that owns the UI simultaneously updates the control. ForSetWindowText
, this may not be a problem because the function does the same thing of sending theWM_SETTEXT
message.«_Superman_» I love work. It gives me something to do between weekends.
«_Superman_» wrote:
The right way to do it is to post messages to the thread that owns the UI.
You mean I should post the message straight to the control right?
«_Superman_» wrote:
For SetWindowText, this may not be a problem because the function does the same thing of sending the WM_SETTEXT message.
You mean I can go ahead with myCtrol->SetWindowText()? Then which controls/APIs exactly make the problem?
---------------------------- 286? WOWW!:-O
-
«_Superman_» wrote:
The right way to do it is to post messages to the thread that owns the UI.
You mean I should post the message straight to the control right?
«_Superman_» wrote:
For SetWindowText, this may not be a problem because the function does the same thing of sending the WM_SETTEXT message.
You mean I can go ahead with myCtrol->SetWindowText()? Then which controls/APIs exactly make the problem?
---------------------------- 286? WOWW!:-O
_8086 wrote:
You mean I should post the message straight to the control right?
No. Create a custom message and post the custom message to the UI thread. The custom message handler in the UI thread can call
SetWindowText
._8086 wrote:
You mean I can go ahead with myCtrol->SetWindowText()?
It is cleaner to send a custom message to the UI thread.
«_Superman_» I love work. It gives me something to do between weekends.
-
I have a worker thread for example, DWORD ThreadProc(void* pVal) { while() { pcEdit->SetWindowText("Status"); pcEdit2->SetWindowText("Status2"); } } Is it okay to update UI this way from the thread? Or how should I do? Anybody can explain in simple words? The above actually works fine. But mate says I shouldn't do it, but he doesn't say how to do it. :doh:
---------------------------- 286? WOWW!:-O
Here[^] is an excellent article you should read before starting to work with threads. It will cover a lot of important points and traps when working with thread (it also has a section answering your question).
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
I have a worker thread for example, DWORD ThreadProc(void* pVal) { while() { pcEdit->SetWindowText("Status"); pcEdit2->SetWindowText("Status2"); } } Is it okay to update UI this way from the thread? Or how should I do? Anybody can explain in simple words? The above actually works fine. But mate says I shouldn't do it, but he doesn't say how to do it. :doh:
---------------------------- 286? WOWW!:-O
I hope you've resolved the problem by now? However, take this as a rule while doing multi-threaded programming: Never manipulate an UI element from a different thread than what owns it instead. Like others said, post a message to the thread that owns the UI element, and have it manipulate the control for you.
It is a crappy thing, but it's life -^ Carlo Pallini