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. suspending program execution. kbhit()

suspending program execution. kbhit()

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
19 Posts 6 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.
  • 9 Offline
    9 Offline
    9ine
    wrote on last edited by
    #1

    I use kbhit for suspending program execution but it consumes processor cycles while(!kbhit()){;} how to do it without consuming cycles

    9ine

    T K E 3 Replies Last reply
    0
    • 9 9ine

      I use kbhit for suspending program execution but it consumes processor cycles while(!kbhit()){;} how to do it without consuming cycles

      9ine

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

      #include <conio.h>

      getch();


      TOXCCT >>> GEII power

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

      1 Reply Last reply
      0
      • 9 9ine

        I use kbhit for suspending program execution but it consumes processor cycles while(!kbhit()){;} how to do it without consuming cycles

        9ine

        K Offline
        K Offline
        kakan
        wrote on last edited by
        #3

        One way: while(!kbhit()){Sleep(100);}

        Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

        T 9 2 Replies Last reply
        0
        • K kakan

          One way: while(!kbhit()){Sleep(100);}

          Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

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

          bad idea... it will only delay the consume time each 100 milliseconds. in a general mean, one should avoid the use of sleep-like methods...


          TOXCCT >>> GEII power

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

          K 2 Replies Last reply
          0
          • T toxcct

            bad idea... it will only delay the consume time each 100 milliseconds. in a general mean, one should avoid the use of sleep-like methods...


            TOXCCT >>> GEII power

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

            K Offline
            K Offline
            kakan
            wrote on last edited by
            #5

            And what does getch() do, while it's waiting for a keystroke?

            Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

            E T R 3 Replies Last reply
            0
            • 9 9ine

              I use kbhit for suspending program execution but it consumes processor cycles while(!kbhit()){;} how to do it without consuming cycles

              9ine

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #6

              It's not your kbhit to consume your processor cycles, but your "while" loop.


              --[:jig:]-- [My Current Status]

              9 1 Reply Last reply
              0
              • K kakan

                And what does getch() do, while it's waiting for a keystroke?

                Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                E Offline
                E Offline
                Eytukan
                wrote on last edited by
                #7

                I think both use the interrupts.


                --[:jig:]-- [My Current Status]

                1 Reply Last reply
                0
                • K kakan

                  And what does getch() do, while it's waiting for a keystroke?

                  Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

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

                  keyboard interrupt... it hibernated the process, until a key is pressed. it doesn't loop (with a sleep call to reduce to proc consume) watching if a key is pressed. moreover, your method will fail if the key is pressed and released while being in the sleep call.


                  TOXCCT >>> GEII power

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

                  1 Reply Last reply
                  0
                  • K kakan

                    And what does getch() do, while it's waiting for a keystroke?

                    Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                    R Offline
                    R Offline
                    Rage
                    wrote on last edited by
                    #9

                    int __cdecl _getch (
                    void
                    )
                    {
                    INPUT_RECORD ConInpRec;
                    DWORD NumRead;
                    CharPair *pCP;
                    int ch = 0; /* single character buffer */
                    DWORD oldstate;

                        /\*
                         \* check pushback buffer (chbuf) a for character
                         \*/
                        if ( chbuf != EOF ) {
                            /\*
                             \* something there, clear buffer and return the character.
                             \*/
                            ch = (unsigned char)(chbuf & 0xFF);
                            chbuf = EOF;
                            return ch;
                        }
                    
                        if (\_coninpfh == -1)
                            return EOF;
                    
                        /\*
                         \* \_coninpfh, the handle to the console input, is created the first
                         \* time that either \_getch() or \_cgets() or \_kbhit() is called.
                         \*/
                    
                        if ( \_coninpfh == -2 )
                            \_\_initconin();
                    
                        /\*
                         \* Switch to raw mode (no line input, no echo input)
                         \*/
                        GetConsoleMode( (HANDLE)\_coninpfh, &oldstate );
                        SetConsoleMode( (HANDLE)\_coninpfh, 0L );
                    
                        for ( ; ; ) {
                    
                            /\*
                             \* Get a console input event.
                             \*/
                            if ( !ReadConsoleInput( (HANDLE)\_coninpfh,
                                                    &ConInpRec,
                                                    1L,
                                                    &NumRead )
                                 || (NumRead == 0L) )
                            {
                                ch = EOF;
                                break;
                            }
                    
                            /\*
                             \* Look for, and decipher, key events.
                             \*/
                            if ( (ConInpRec.EventType == KEY\_EVENT) &&
                                 ConInpRec.Event.KeyEvent.bKeyDown ) {
                                /\*
                                 \* Easy case: if uChar.AsciiChar is non-zero, just stuff it
                                 \* into ch and quit.
                                 \*/
                    
                                if ( ch = (unsigned char)ConInpRec.Event.KeyEvent.uChar.AsciiChar )
                                    break;
                    
                                /\*
                                 \* Hard case: either an extended code or an event which should
                                 \* not be recognized. let \_getextendedkeycode() do the work...
                                 \*/
                                if ( pCP = \_getextendedkeycode( &(ConInpRec.Event.KeyEvent) ) ) {
                                    ch = pCP->LeadChar;
                                    chbuf = pCP->SecondChar;
                                    break;
                                }
                            }
                        }
                    
                    
                        /\*
                         \* Restore previous console mode.
                         \*/
                        SetConsoleMode( (HANDLE)\_coninpfh, oldstate );
                    
                        return ch;
                    

                    }

                    :);)

                    T 1 Reply Last reply
                    0
                    • R Rage

                      int __cdecl _getch (
                      void
                      )
                      {
                      INPUT_RECORD ConInpRec;
                      DWORD NumRead;
                      CharPair *pCP;
                      int ch = 0; /* single character buffer */
                      DWORD oldstate;

                          /\*
                           \* check pushback buffer (chbuf) a for character
                           \*/
                          if ( chbuf != EOF ) {
                              /\*
                               \* something there, clear buffer and return the character.
                               \*/
                              ch = (unsigned char)(chbuf & 0xFF);
                              chbuf = EOF;
                              return ch;
                          }
                      
                          if (\_coninpfh == -1)
                              return EOF;
                      
                          /\*
                           \* \_coninpfh, the handle to the console input, is created the first
                           \* time that either \_getch() or \_cgets() or \_kbhit() is called.
                           \*/
                      
                          if ( \_coninpfh == -2 )
                              \_\_initconin();
                      
                          /\*
                           \* Switch to raw mode (no line input, no echo input)
                           \*/
                          GetConsoleMode( (HANDLE)\_coninpfh, &oldstate );
                          SetConsoleMode( (HANDLE)\_coninpfh, 0L );
                      
                          for ( ; ; ) {
                      
                              /\*
                               \* Get a console input event.
                               \*/
                              if ( !ReadConsoleInput( (HANDLE)\_coninpfh,
                                                      &ConInpRec,
                                                      1L,
                                                      &NumRead )
                                   || (NumRead == 0L) )
                              {
                                  ch = EOF;
                                  break;
                              }
                      
                              /\*
                               \* Look for, and decipher, key events.
                               \*/
                              if ( (ConInpRec.EventType == KEY\_EVENT) &&
                                   ConInpRec.Event.KeyEvent.bKeyDown ) {
                                  /\*
                                   \* Easy case: if uChar.AsciiChar is non-zero, just stuff it
                                   \* into ch and quit.
                                   \*/
                      
                                  if ( ch = (unsigned char)ConInpRec.Event.KeyEvent.uChar.AsciiChar )
                                      break;
                      
                                  /\*
                                   \* Hard case: either an extended code or an event which should
                                   \* not be recognized. let \_getextendedkeycode() do the work...
                                   \*/
                                  if ( pCP = \_getextendedkeycode( &(ConInpRec.Event.KeyEvent) ) ) {
                                      ch = pCP->LeadChar;
                                      chbuf = pCP->SecondChar;
                                      break;
                                  }
                              }
                          }
                      
                      
                          /\*
                           \* Restore previous console mode.
                           \*/
                          SetConsoleMode( (HANDLE)\_coninpfh, oldstate );
                      
                          return ch;
                      

                      }

                      :);)

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

                      of course :doh::rolleyes:


                      TOXCCT >>> GEII power

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

                      1 Reply Last reply
                      0
                      • K kakan

                        One way: while(!kbhit()){Sleep(100);}

                        Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                        9 Offline
                        9 Offline
                        9ine
                        wrote on last edited by
                        #11

                        no it is not proper

                        9ine

                        K 1 Reply Last reply
                        0
                        • E Eytukan

                          It's not your kbhit to consume your processor cycles, but your "while" loop.


                          --[:jig:]-- [My Current Status]

                          9 Offline
                          9 Offline
                          9ine
                          wrote on last edited by
                          #12

                          so no useful ones for program suspension what about gets() but how to not to print the key you pressed?

                          9ine

                          T 1 Reply Last reply
                          0
                          • 9 9ine

                            no it is not proper

                            9ine

                            K Offline
                            K Offline
                            kakan
                            wrote on last edited by
                            #13

                            It's better than writing code with 100% CPU utilisation... :-D

                            Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                            E 1 Reply Last reply
                            0
                            • 9 9ine

                              so no useful ones for program suspension what about gets() but how to not to print the key you pressed?

                              9ine

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

                              hey, did you read my answer ? getch() works perfectly.


                              TOXCCT >>> GEII power

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

                              1 Reply Last reply
                              0
                              • T toxcct

                                bad idea... it will only delay the consume time each 100 milliseconds. in a general mean, one should avoid the use of sleep-like methods...


                                TOXCCT >>> GEII power

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

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

                                Sorry for bringing this up again, but... If my eyes serves me right (still), the question was this (I quote) "I use kbhit for suspending program execution but it consumes processor cycles while(!kbhit()){;} how to do it without consuming cycles" Of course, a Sleep (generally speaking) isn't the best of ways to lessen CPU utilization, but the question was how to lessen CPU utilization when using the kbhit() function, especially (as in this case) when kbhit() returns 0. So, can you come up with a better solution to the question asked? Yeah, I know about getc, getch, getch e.t.c. but that wasn't the question! How would you minimize the CPU utilization when you use kbhit() to wait for a key press?

                                Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                                T 1 Reply Last reply
                                0
                                • K kakan

                                  Sorry for bringing this up again, but... If my eyes serves me right (still), the question was this (I quote) "I use kbhit for suspending program execution but it consumes processor cycles while(!kbhit()){;} how to do it without consuming cycles" Of course, a Sleep (generally speaking) isn't the best of ways to lessen CPU utilization, but the question was how to lessen CPU utilization when using the kbhit() function, especially (as in this case) when kbhit() returns 0. So, can you come up with a better solution to the question asked? Yeah, I know about getc, getch, getch e.t.c. but that wasn't the question! How would you minimize the CPU utilization when you use kbhit() to wait for a key press?

                                  Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

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

                                  kakan wrote:

                                  the question was

                                  yes, but mine is this one : why still wanting to use a function that doesn't fit the best to your needs ?


                                  TOXCCT >>> GEII power

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

                                  D K 2 Replies Last reply
                                  0
                                  • T toxcct

                                    kakan wrote:

                                    the question was

                                    yes, but mine is this one : why still wanting to use a function that doesn't fit the best to your needs ?


                                    TOXCCT >>> GEII power

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

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

                                    toxcct wrote:

                                    why still wanting to use a function that doesn't fit the best to your needs ?

                                    I agree. Why try to solve a problem that does not need to exist? Nothing like trying to pound a square peg into a round hole!


                                    "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

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

                                    1 Reply Last reply
                                    0
                                    • T toxcct

                                      kakan wrote:

                                      the question was

                                      yes, but mine is this one : why still wanting to use a function that doesn't fit the best to your needs ?


                                      TOXCCT >>> GEII power

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

                                      K Offline
                                      K Offline
                                      kakan
                                      wrote on last edited by
                                      #18

                                      You are right. He wanted a solution to the problem, which you gave him. I gave an answer to his question. But still, it's an interesting question: How to use kbhit in an efficient manner?

                                      Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                                      1 Reply Last reply
                                      0
                                      • K kakan

                                        It's better than writing code with 100% CPU utilisation... :-D

                                        Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                                        E Offline
                                        E Offline
                                        Eytukan
                                        wrote on last edited by
                                        #19

                                        I think no one understood your joke. :-D


                                        --[:jig:]-- [My Current Status] Link2006 wrote:Let's take it outside of CP Jeremy : Please don't.I would love to see this.I'm making the popcorn already.

                                        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