Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Updating UI from thread

Updating UI from thread

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialdesignquestionannouncement
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _8086
    wrote on last edited by
    #1

    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

    _ C R 3 Replies Last reply
    0
    • _ _8086

      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

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      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. For SetWindowText, this may not be a problem because the function does the same thing of sending the WM_SETTEXT message.

      «_Superman_» I love work. It gives me something to do between weekends.

      _ 1 Reply Last reply
      0
      • _ _Superman_

        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. For SetWindowText, this may not be a problem because the function does the same thing of sending the WM_SETTEXT message.

        «_Superman_» I love work. It gives me something to do between weekends.

        _ Offline
        _ Offline
        _8086
        wrote on last edited by
        #3

        «_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

        _ 1 Reply Last reply
        0
        • _ _8086

          «_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

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          _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.

          1 Reply Last reply
          0
          • _ _8086

            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

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            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++

            1 Reply Last reply
            0
            • _ _8086

              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

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups