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. Problems with Dialog Buttons

Problems with Dialog Buttons

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
16 Posts 8 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.
  • K Offline
    K Offline
    kitty5
    wrote on last edited by
    #1

    I've created a Dialog that after some input collects data until the buffer is full or the user click on the Stop button. The problem I'm having is I have a while loop the does the data collection code and once my gui has entered this loop all buttons in the dialog box have no functionality...:mad: (I can't click on anything). How would I check to see if the user has clicked on the "stop" button? At 1st I thought maybe: while( ... || ... || (m_nStopBtn.GetCheck() ) { ... } but that didn't work since the gui won't let me click on the stop button... What :wtf: could I be doing wrong? Thanks,

    Kitty5

    T V K R 4 Replies Last reply
    0
    • K kitty5

      I've created a Dialog that after some input collects data until the buffer is full or the user click on the Stop button. The problem I'm having is I have a while loop the does the data collection code and once my gui has entered this loop all buttons in the dialog box have no functionality...:mad: (I can't click on anything). How would I check to see if the user has clicked on the "stop" button? At 1st I thought maybe: while( ... || ... || (m_nStopBtn.GetCheck() ) { ... } but that didn't work since the gui won't let me click on the stop button... What :wtf: could I be doing wrong? Thanks,

      Kitty5

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      you shouldn't put huge processes into the GUI thread... move that loop into a worker thread to avoid the GUI to be frozen


      TOXCCT >>> GEII power

      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

      Z K 2 Replies Last reply
      0
      • T toxcct

        you shouldn't put huge processes into the GUI thread... move that loop into a worker thread to avoid the GUI to be frozen


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

        Z Offline
        Z Offline
        Zac Howland
        wrote on last edited by
        #3

        You beat me to that answer :P Also, make sure you use proper synchronization objects when doing this (do NOT just try to check a boolean variable each iteration of the loop to see if you should stop!).

        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

        T K 2 Replies Last reply
        0
        • T toxcct

          you shouldn't put huge processes into the GUI thread... move that loop into a worker thread to avoid the GUI to be frozen


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          K Offline
          K Offline
          kitty5
          wrote on last edited by
          #4

          I'm new with creating MFC Application GUIs... please pardon the stupid question that follows.... What do you mean when you say:

          toxcct wrote:

          move that loop into a worker thread to avoid the GUI to be frozen

          ? Do I create a separate class for the worker thread and have the GUI call it? The while loop is using member variables of the GUI...(i.e. checking to see whatever user inputs are. thanks so much,

          Kitty5

          T D 2 Replies Last reply
          0
          • Z Zac Howland

            You beat me to that answer :P Also, make sure you use proper synchronization objects when doing this (do NOT just try to check a boolean variable each iteration of the loop to see if you should stop!).

            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            :->

            Zac Howland wrote:

            NOT just try to check a boolean variable each iteration of the loop to see if you should stop!

            of course ;)


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            1 Reply Last reply
            0
            • Z Zac Howland

              You beat me to that answer :P Also, make sure you use proper synchronization objects when doing this (do NOT just try to check a boolean variable each iteration of the loop to see if you should stop!).

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              K Offline
              K Offline
              kitty5
              wrote on last edited by
              #6

              pardon my stupidity but....

              Zac Howland wrote:

              proper synchronization objects

              :confused:? Thanks,

              Kitty5

              T Z 2 Replies Last reply
              0
              • K kitty5

                I'm new with creating MFC Application GUIs... please pardon the stupid question that follows.... What do you mean when you say:

                toxcct wrote:

                move that loop into a worker thread to avoid the GUI to be frozen

                ? Do I create a separate class for the worker thread and have the GUI call it? The while loop is using member variables of the GUI...(i.e. checking to see whatever user inputs are. thanks so much,

                Kitty5

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                actually, the code is already executing in a thread (threads are some executing parts managed by the system). when you put the loop in the event handler, the execution leaves the event handler only when the loop exits and all the instructions where executed. the problem with suc a design is that other event handlers (such as a button click - yeah, your stop for example) are pushed in the application events stack, but not poped until the current event is not finished. then, what you can do for that is saying to the system that the loop will be executed in parallel. i mean, the event handler starts the loop, but doesn't wait for its end. the system doesn't bother if the loop is using member variabless or not, because threads of a same process share the same memory...


                TOXCCT >>> GEII power

                [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                J 1 Reply Last reply
                0
                • K kitty5

                  pardon my stupidity but....

                  Zac Howland wrote:

                  proper synchronization objects

                  :confused:? Thanks,

                  Kitty5

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

                  zac meant that as several threads access the same memory (see my other answer for the beginning of the discussion), two (or more) threads can access and modify the same variable at the same time. very bad idea !!! the solution for this is to "synchronize" the objects that are shared to avoid such problems/ google for semaphores and/or mutex for example


                  TOXCCT >>> GEII power

                  [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                  1 Reply Last reply
                  0
                  • K kitty5

                    I've created a Dialog that after some input collects data until the buffer is full or the user click on the Stop button. The problem I'm having is I have a while loop the does the data collection code and once my gui has entered this loop all buttons in the dialog box have no functionality...:mad: (I can't click on anything). How would I check to see if the user has clicked on the "stop" button? At 1st I thought maybe: while( ... || ... || (m_nStopBtn.GetCheck() ) { ... } but that didn't work since the gui won't let me click on the stop button... What :wtf: could I be doing wrong? Thanks,

                    Kitty5

                    V Offline
                    V Offline
                    valikac
                    wrote on last edited by
                    #9

                    AfxBeginThread() windows platform: process is comprised of main thread (UI thread on client-side) and can spawn additional threads (UI and/or worker) typically single cpu can process one thread any given moment and in this example spawn a worker thread to process data Kuphryn

                    1 Reply Last reply
                    0
                    • K kitty5

                      I've created a Dialog that after some input collects data until the buffer is full or the user click on the Stop button. The problem I'm having is I have a while loop the does the data collection code and once my gui has entered this loop all buttons in the dialog box have no functionality...:mad: (I can't click on anything). How would I check to see if the user has clicked on the "stop" button? At 1st I thought maybe: while( ... || ... || (m_nStopBtn.GetCheck() ) { ... } but that didn't work since the gui won't let me click on the stop button... What :wtf: could I be doing wrong? Thanks,

                      Kitty5

                      K Offline
                      K Offline
                      kitty5
                      wrote on last edited by
                      #10

                      Thanks everyone! All the answers have been a great help! now I've got a lot of reading ahead of me! :-D

                      Kitty5

                      H 1 Reply Last reply
                      0
                      • T toxcct

                        actually, the code is already executing in a thread (threads are some executing parts managed by the system). when you put the loop in the event handler, the execution leaves the event handler only when the loop exits and all the instructions where executed. the problem with suc a design is that other event handlers (such as a button click - yeah, your stop for example) are pushed in the application events stack, but not poped until the current event is not finished. then, what you can do for that is saying to the system that the loop will be executed in parallel. i mean, the event handler starts the loop, but doesn't wait for its end. the system doesn't bother if the loop is using member variabless or not, because threads of a same process share the same memory...


                        TOXCCT >>> GEII power

                        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                        J Offline
                        J Offline
                        Justin Tay
                        wrote on last edited by
                        #11

                        I think you meant to say queue instead of stack ;)

                        1 Reply Last reply
                        0
                        • K kitty5

                          pardon my stupidity but....

                          Zac Howland wrote:

                          proper synchronization objects

                          :confused:? Thanks,

                          Kitty5

                          Z Offline
                          Z Offline
                          Zac Howland
                          wrote on last edited by
                          #12

                          Basically, what you will want is to create a worker thread (as mentioned above). As part of the creation, you will want to pass in a variable that contains a handle to an event (typically, you will want to pass much more than just that, so a structure/class is created to hold everything you need to pass and then you pass a pointer to that object to the thread you are creating). Each iteration (or some on some interval) of your loop, your worker thread will call WaitForSingleObject and check to see if the event is signaled. If it is not, it continues doing its thing. During the whole loop, you should lock the data section you will be writing to using at least one critical section (which is a form of semaphore that Windows gives you). This way, your GUI thread (or any other thread for that matter -- all of which should also be using critical sections to access the shared data) cannot try to read/write to the memory that you are still loading. If all of that sounds greek to you, read up on multithreaded programming in C/C++ (lots of good articles on here and Google can be a great help as well). Unless you are going to be working with other OS's, you can focus your search to Windows specific articles.

                          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                          1 Reply Last reply
                          0
                          • K kitty5

                            I'm new with creating MFC Application GUIs... please pardon the stupid question that follows.... What do you mean when you say:

                            toxcct wrote:

                            move that loop into a worker thread to avoid the GUI to be frozen

                            ? Do I create a separate class for the worker thread and have the GUI call it? The while loop is using member variables of the GUI...(i.e. checking to see whatever user inputs are. thanks so much,

                            Kitty5

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

                            See here.


                            "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                            "Judge not by the eye but by the heart." - Native American Proverb

                            1 Reply Last reply
                            0
                            • K kitty5

                              I've created a Dialog that after some input collects data until the buffer is full or the user click on the Stop button. The problem I'm having is I have a while loop the does the data collection code and once my gui has entered this loop all buttons in the dialog box have no functionality...:mad: (I can't click on anything). How would I check to see if the user has clicked on the "stop" button? At 1st I thought maybe: while( ... || ... || (m_nStopBtn.GetCheck() ) { ... } but that didn't work since the gui won't let me click on the stop button... What :wtf: could I be doing wrong? Thanks,

                              Kitty5

                              R Offline
                              R Offline
                              RChin
                              wrote on last edited by
                              #14

                              I do agree that ideally you should employ the techniques suggested above, using worker threads. But sometimes a person does not have the time (or experience) for this route to be feasable. I often use the pump message technique (equivalent in VB as DoEvents()) to prevent the UI freezing and also allow the user to interrupt lengthy sessions. Have a look at the FAQ[^], under section 4.3 I have a dialog that does some lengthy processing, and I need to have a Cancel button so the user can abort the processing. How do I get the Cancel button to work?


                              I Dream of Absolute Zero

                              Z 1 Reply Last reply
                              0
                              • R RChin

                                I do agree that ideally you should employ the techniques suggested above, using worker threads. But sometimes a person does not have the time (or experience) for this route to be feasable. I often use the pump message technique (equivalent in VB as DoEvents()) to prevent the UI freezing and also allow the user to interrupt lengthy sessions. Have a look at the FAQ[^], under section 4.3 I have a dialog that does some lengthy processing, and I need to have a Cancel button so the user can abort the processing. How do I get the Cancel button to work?


                                I Dream of Absolute Zero

                                Z Offline
                                Z Offline
                                Zac Howland
                                wrote on last edited by
                                #15

                                RChin wrote:

                                I often use the pump message technique (equivalent in VB as DoEvents()) to prevent the UI freezing and also allow the user to interrupt lengthy sessions.

                                While I can understand not wanting to create tons of threads all over the place, creating what amounts to message pumps all over the place is a much worse practice and should be avoided (even in VB).

                                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                1 Reply Last reply
                                0
                                • K kitty5

                                  Thanks everyone! All the answers have been a great help! now I've got a lot of reading ahead of me! :-D

                                  Kitty5

                                  H Offline
                                  H Offline
                                  Hamid Taebi
                                  wrote on last edited by
                                  #16

                                  See AfxBeginThread[^]

                                  _**


                                  **_

                                  WhiteSky


                                  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