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. MFC UI Thread vs. Background Thread

MFC UI Thread vs. Background Thread

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++visual-studiodesign
6 Posts 5 Posters 2 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    What is the MFC mechanism for accessing UI controls from background threads? In C#, we use "InvokeRequired" and "BeginInvoke" to schedule a function call on the UI thread, but how is that done in MFC?

    The difficult we do right away... ...the impossible takes slightly longer.

    _ V L 3 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      What is the MFC mechanism for accessing UI controls from background threads? In C#, we use "InvokeRequired" and "BeginInvoke" to schedule a function call on the UI thread, but how is that done in MFC?

      The difficult we do right away... ...the impossible takes slightly longer.

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

      In MFC, you should not directly access UI elements from background threads. Instead, you must send messages to the UI thread to get and set elements for the UI. Use can use SendMessage or PostMessage.

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

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      R 1 Reply Last reply
      0
      • _ _Superman_

        In MFC, you should not directly access UI elements from background threads. Instead, you must send messages to the UI thread to get and set elements for the UI. Use can use SendMessage or PostMessage.

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

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        R Offline
        R Offline
        Roger Stoltz
        wrote on last edited by
        #3

        «_Superman_» wrote:

        In MFC, you should not directly access UI elements from background threads.

        True.

        «_Superman_» wrote:

        Instead, you must send messages to the UI thread to get and set elements for the UI.
        Use can use SendMessage or PostMessage.

        Not entirely true. The reason for not accessing UI elements from another thread than the main thread is that you can quite easily create a deadlock situation. MFC classes for UI elements uses ::SendMessage() which will block until the message has been handled. If the main thread is not processing messages, possibly waiting for the thread that manipulates the UI element, the application will deadlock. This of course means that you cannot use ::SendMessage(), even if you call it directly, as it would create the same potential deadlock situation. ::PostMessage() must be used since it doesn't wait for the message to be handled. It is possible to use ::SendMessageTimeout() to avoid a deadlock situation, but if the call fails the receiving thread never gets the message.

        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
        "High speed never compensates for wrong direction!" - unknown

        Richard Andrew x64R 1 Reply Last reply
        0
        • R Roger Stoltz

          «_Superman_» wrote:

          In MFC, you should not directly access UI elements from background threads.

          True.

          «_Superman_» wrote:

          Instead, you must send messages to the UI thread to get and set elements for the UI.
          Use can use SendMessage or PostMessage.

          Not entirely true. The reason for not accessing UI elements from another thread than the main thread is that you can quite easily create a deadlock situation. MFC classes for UI elements uses ::SendMessage() which will block until the message has been handled. If the main thread is not processing messages, possibly waiting for the thread that manipulates the UI element, the application will deadlock. This of course means that you cannot use ::SendMessage(), even if you call it directly, as it would create the same potential deadlock situation. ::PostMessage() must be used since it doesn't wait for the message to be handled. It is possible to use ::SendMessageTimeout() to avoid a deadlock situation, but if the call fails the receiving thread never gets the message.

          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
          "High speed never compensates for wrong direction!" - unknown

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          Thanks for clarifying that!

          The difficult we do right away... ...the impossible takes slightly longer.

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            What is the MFC mechanism for accessing UI controls from background threads? In C#, we use "InvokeRequired" and "BeginInvoke" to schedule a function call on the UI thread, but how is that done in MFC?

            The difficult we do right away... ...the impossible takes slightly longer.

            V Offline
            V Offline
            Vitaly Tomilov
            wrote on last edited by
            #5

            SendMessage and PostMessage will do the job. You may also like to use PostThreadMessage instead of PostMessage ;)

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              What is the MFC mechanism for accessing UI controls from background threads? In C#, we use "InvokeRequired" and "BeginInvoke" to schedule a function call on the UI thread, but how is that done in MFC?

              The difficult we do right away... ...the impossible takes slightly longer.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              If you pass the window handle that contains the UI you want to manipulate to the thread when created you can use it in ::PostMessage().

              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