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. how to stop run of a program after a message display [modified]

how to stop run of a program after a message display [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
15 Posts 7 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.
  • M Offline
    M Offline
    mrby123
    wrote on last edited by
    #1

    I want to stop the program run after the message box display int npoints=(tmax+1000)/sam_interval; if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); } With the above statement, after I close the message box, the program resumes the run. In fact, I want to stop the program after closing the message box. Please give me help Thanks

    modified on Wednesday, May 19, 2010 1:16 PM

    D L M A K 5 Replies Last reply
    0
    • M mrby123

      I want to stop the program run after the message box display int npoints=(tmax+1000)/sam_interval; if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); } With the above statement, after I close the message box, the program resumes the run. In fact, I want to stop the program after closing the message box. Please give me help Thanks

      modified on Wednesday, May 19, 2010 1:16 PM

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

      How about sending it a WM_CLOSE message?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Man who follows car will be exhausted." - Confucius

      M 1 Reply Last reply
      0
      • M mrby123

        I want to stop the program run after the message box display int npoints=(tmax+1000)/sam_interval; if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); } With the above statement, after I close the message box, the program resumes the run. In fact, I want to stop the program after closing the message box. Please give me help Thanks

        modified on Wednesday, May 19, 2010 1:16 PM

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        mrby123 wrote:

        Please select a larger sampling interval.

        That message does not make sense if your app is exiting anyway. :(

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        M 1 Reply Last reply
        0
        • D David Crow

          How about sending it a WM_CLOSE message?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          M Offline
          M Offline
          mrby123
          wrote on last edited by
          #4

          Thanks. I use exit(-1); after the message box. it works. if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); exit(-1); }

          D 1 Reply Last reply
          0
          • M mrby123

            I want to stop the program run after the message box display int npoints=(tmax+1000)/sam_interval; if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); } With the above statement, after I close the message box, the program resumes the run. In fact, I want to stop the program after closing the message box. Please give me help Thanks

            modified on Wednesday, May 19, 2010 1:16 PM

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            Well, maybe you should just "return" from the current flow of execution and let the user enter a new interval instead of just closing the application!! something like :

            while (sample not good )
            {
            enter sampling.
            compute value.
            validate value.
            }

            M.

            Watched code never compiles.

            M 1 Reply Last reply
            0
            • M mrby123

              Thanks. I use exit(-1); after the message box. it works. if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); exit(-1); }

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

              mrby123 wrote:

              I use exit(-1)...

              To exit a Windows GUI application? :confused: Not a good choice.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Man who follows car will be exhausted." - Confucius

              1 Reply Last reply
              0
              • L Luc Pattyn

                mrby123 wrote:

                Please select a larger sampling interval.

                That message does not make sense if your app is exiting anyway. :(

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                M Offline
                M Offline
                mrby123
                wrote on last edited by
                #7

                When you program suddenly stop, you may want to inform the use what happen. Otherwise, the use feels not good.

                L 1 Reply Last reply
                0
                • M Maximilien

                  Well, maybe you should just "return" from the current flow of execution and let the user enter a new interval instead of just closing the application!! something like :

                  while (sample not good )
                  {
                  enter sampling.
                  compute value.
                  validate value.
                  }

                  M.

                  Watched code never compiles.

                  M Offline
                  M Offline
                  mrby123
                  wrote on last edited by
                  #8

                  I am running MFC C++ application. Your suggestion is good. I do not know which dialoge box should I use to do such interaction. Thanks again

                  1 Reply Last reply
                  0
                  • M mrby123

                    When you program suddenly stop, you may want to inform the use what happen. Otherwise, the use feels not good.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    I agree, but then you should choose a more accurate message; maybe "next time you run this program, please enter ...". Although I still think it is rather rude to stop a program just because the interactive user entered something you did not like; you should allow for another chance. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    M 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      I agree, but then you should choose a more accurate message; maybe "next time you run this program, please enter ...". Although I still think it is rather rude to stop a program just because the interactive user entered something you did not like; you should allow for another chance. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                      M Offline
                      M Offline
                      mrby123
                      wrote on last edited by
                      #10

                      I agree with you, it is better to give user a chance to correct. I wish to find an interactive message box to allow user to inter a correct value for the parameter. Do anyone know how to program an interactive dialogue box? Thanks again

                      _ L 2 Replies Last reply
                      0
                      • M mrby123

                        I agree with you, it is better to give user a chance to correct. I wish to find an interactive message box to allow user to inter a correct value for the parameter. Do anyone know how to program an interactive dialogue box? Thanks again

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

                        Assuming you're using MFC...

                        If (IDYES == MessageBox(_T("Are you sure you want to quit"), _T("Confirm Exit"), MB_YESNO))
                        {
                        PostQuitMessage(0);
                        }

                        «_Superman_» I love work. It gives me something to do between weekends.
                        Microsoft MVP (Visual C++)
                        Polymorphism in C

                        1 Reply Last reply
                        0
                        • M mrby123

                          I agree with you, it is better to give user a chance to correct. I wish to find an interactive message box to allow user to inter a correct value for the parameter. Do anyone know how to program an interactive dialogue box? Thanks again

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          you may want to implement this pseudo-code:

                          string s="Please enter a number in the range [a,b]"
                          for(;;) {
                          int n=NumericInputBox.Show(s);
                          if (n==-1) Application.Exit();
                          if (n>=a && n<=b) break;
                          s="Sorry, "+n+" is not valid as it is not in the range [a,b]; please try again now (or enter -1 to quit)";
                          }

                          :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                          I only read formatted code with indentation, so please use PRE tags for code snippets.


                          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                          M 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            you may want to implement this pseudo-code:

                            string s="Please enter a number in the range [a,b]"
                            for(;;) {
                            int n=NumericInputBox.Show(s);
                            if (n==-1) Application.Exit();
                            if (n>=a && n<=b) break;
                            s="Sorry, "+n+" is not valid as it is not in the range [a,b]; please try again now (or enter -1 to quit)";
                            }

                            :)

                            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                            I only read formatted code with indentation, so please use PRE tags for code snippets.


                            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                            M Offline
                            M Offline
                            mrby123
                            wrote on last edited by
                            #13

                            Hi Thanks. I will try it. Another Key is to make sure the app can catch the new value entered in the dialogue box. Thanks for any specific instructions.

                            1 Reply Last reply
                            0
                            • M mrby123

                              I want to stop the program run after the message box display int npoints=(tmax+1000)/sam_interval; if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); } With the above statement, after I close the message box, the program resumes the run. In fact, I want to stop the program after closing the message box. Please give me help Thanks

                              modified on Wednesday, May 19, 2010 1:16 PM

                              A Offline
                              A Offline
                              Aescleal
                              wrote on last edited by
                              #14

                              Hi, As you're using an MFC app look up Dialog Data Exchange (DDX) and Dialog Data Validation (DDV). Between the pair of them you can specify allowed ranges for field in a dialog box and give the user a message if they enter something that's out of the range. Out of the box it's a bit clunky but works okay with a bit of tender loving care. So if you've got an edit control with the sampling interval in milliseconds (IDC_SAMPLE_INTERVAL) and you want the user to be able to enter something between 10 and 1000ms, you overide DoDataExchange in your dialogue box class as something like:

                              void CSampleDlg::DoDataExchange( CDataExchange *pDX )
                              {
                              CDialog::DoDataExchange( pDX );

                              DDX\_Text( pDX, IDC\_SAMPLE\_INTERVAL, sample\_interval\_ );
                              DDV\_MinMaxInt( pDX, sample\_interval\_, 10, 1000 ); 
                              

                              }

                              where sample_interval_ is the data member containing your er, sample interval. If the entered interval isn't between 10 and 1000 MFC displays an message box and lets the user choose to abandon the edit or try again. Cheers, Ash PS: Using exit() in a C++ program can be a bad idea one main reason - destructors aren't run for any objects. If those objects are managing a system global resource then that resource can leak. It can also mean that files aren't flushed and closed properly and may not have the contents you expect. On the other hand exit() can be a quick way to bail out of a misbehaving program with a long shutdown time if that shutdown time is due to a large number of or slow destructors. This is generally regarded as being pretty vile by most C++ experts and only used as a desperate measure.

                              1 Reply Last reply
                              0
                              • M mrby123

                                I want to stop the program run after the message box display int npoints=(tmax+1000)/sam_interval; if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); } With the above statement, after I close the message box, the program resumes the run. In fact, I want to stop the program after closing the message box. Please give me help Thanks

                                modified on Wednesday, May 19, 2010 1:16 PM

                                K Offline
                                K Offline
                                KarstenK
                                wrote on last edited by
                                #15

                                a nice program would check the range and ask for a proper value, before saving the value. -> "Please enter a valid value or quit"

                                Press F1 for help or google it. Greetings from Germany

                                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