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. Multi-tasking and threads

Multi-tasking and threads

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 3 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.
  • T theprinc

    I have a dialog based app which has a progress bar and other controls. I want to process some stuff in the background moving the progress bar, but not blocking the main process thread. I tried using threads but the child threads cannot move the progresss bar even if i pass the progress bar argument to the child thread. Does anybody have any ideas?? Another idea could be create a new process and let it run and use intrprocess communication but still the parent process is blocked I think i am just complicating things because many applications have this functionality Any ideas would be greatly appreciated. Kelvin Chikomo

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #2

    theprinc wrote:

    I tried using threads but the child threads cannot move the progresss bar...

    Nor should they. The secondary thread(s) should be posting a message back to the primary thread (which owns the progress control) to update the UI.


    "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

    T 1 Reply Last reply
    0
    • D David Crow

      theprinc wrote:

      I tried using threads but the child threads cannot move the progresss bar...

      Nor should they. The secondary thread(s) should be posting a message back to the primary thread (which owns the progress control) to update the UI.


      "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

      T Offline
      T Offline
      theprinc
      wrote on last edited by
      #3

      How do you post messages back to the primary thread without blocking the primary thread. Any sample code would help Kelvi Chikomo

      C D 2 Replies Last reply
      0
      • T theprinc

        How do you post messages back to the primary thread without blocking the primary thread. Any sample code would help Kelvi Chikomo

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

        When you start your second thread, you give it the handle to the window to which it will post messages. Then, to post messages, simply use PostMessage which doesn't block the thread.

        1 Reply Last reply
        0
        • T theprinc

          How do you post messages back to the primary thread without blocking the primary thread. Any sample code would help Kelvi Chikomo

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #5

          theprinc wrote:

          How do you post messages...

          By using PostMessage().


          "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

          T 1 Reply Last reply
          0
          • D David Crow

            theprinc wrote:

            How do you post messages...

            By using PostMessage().


            "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

            T Offline
            T Offline
            theprinc
            wrote on last edited by
            #6

            Say I use the PostThreadMessage() method the primary thread would have to have some sort of a loop to keep reading the messages therefore keeping the primary process busy. The secondary thread would be sending lots of messages Kelvin Chikomo

            D 1 Reply Last reply
            0
            • T theprinc

              Say I use the PostThreadMessage() method the primary thread would have to have some sort of a loop to keep reading the messages therefore keeping the primary process busy. The secondary thread would be sending lots of messages Kelvin Chikomo

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #7

              The primary thread, the one that owns the GUI, has a message queue, and a message pump to process messages in that queue. See here for more.

              theprinc wrote:

              The secondary thread would be sending lots of messages

              For just the updating of a progress bar? Even so, it's doubtful you'll exceed the maximum size of the message queue. You might look at using an I/O Completion Port to queue messages to the main GUI thread. See here for an example.


              "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

              T 1 Reply Last reply
              0
              • D David Crow

                The primary thread, the one that owns the GUI, has a message queue, and a message pump to process messages in that queue. See here for more.

                theprinc wrote:

                The secondary thread would be sending lots of messages

                For just the updating of a progress bar? Even so, it's doubtful you'll exceed the maximum size of the message queue. You might look at using an I/O Completion Port to queue messages to the main GUI thread. See here for an example.


                "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

                T Offline
                T Offline
                theprinc
                wrote on last edited by
                #8

                Thanks dude. I am going to try it using peekMessage() in an MFC environment hope it works Kelvin Chikomo

                D 1 Reply Last reply
                0
                • T theprinc

                  Thanks dude. I am going to try it using peekMessage() in an MFC environment hope it works Kelvin Chikomo

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #9

                  theprinc wrote:

                  I am going to try it using peekMessage() in an MFC environment hope it works

                  Why? An application's primary thread already has a message pump. No need to reinvent the wheel.


                  "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

                  T 1 Reply Last reply
                  0
                  • D David Crow

                    theprinc wrote:

                    I am going to try it using peekMessage() in an MFC environment hope it works

                    Why? An application's primary thread already has a message pump. No need to reinvent the wheel.


                    "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

                    T Offline
                    T Offline
                    theprinc
                    wrote on last edited by
                    #10

                    I dont know how to use the message pump do you have any references Kelvin Chikomo

                    D 1 Reply Last reply
                    0
                    • T theprinc

                      I dont know how to use the message pump do you have any references Kelvin Chikomo

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #11

                      For most applications, there is no need to interface with the message pump, or even the message queue. It's all handled nicely for you. See that worker-thread link I referenced earlier.


                      "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

                      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