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. Console app - how to "keep alive" ?

Console app - how to "keep alive" ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++performancetutorial
13 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 MrBean

    I'm developing a Win32 console app in VC++ 6.0 where I have coded a edit box which works fine... however running my console app on Windows 98, my app affects the speed of the computer. I have tracked the problem down to my "main input loop" where my app wait for the user to press a key. The main loop (very simple!) looks something like this :

    BOOL bOK=TRUE;
    int cKey;
    while (bOK)
    {
    // Wait for keystroke...
    while (!_kbhit())
    {
    // Do nothing...
    }

    // Getting the key...
    cKey = \_getch();
    
    // Process the key here...
    // and so on and so on...
    

    }

    So my question is : is there a "proper" way to wait for a input chars in a console app which does not slow down Windows 9x ? Please help, thanks :)

    M Offline
    M Offline
    Michael P Butler
    wrote on last edited by
    #4

    It has been a while since I've done anything like this but, I don't think you need your _kbhit loop. _getch() will wait for a character to be pressed before executing the next statement. This is the MSDN example

    // crt_getch.c
    // compile with: /c
    /* This program reads characters from
    * the keyboard until it receives a 'Y' or 'y'.
    */

    int ch;

    _cputs( "Type 'Y' when finished typing keys: " );
    do
    {
    ch = _getch();
    ch = toupper( ch );
    } while( ch != 'Y' );

    _putch( ch );
    _putch( '\r' ); /* Carriage return */
    _putch( '\n' ); /* Line feed */
    }

    Michael 'War is at best barbarism...Its glory is all moonshine. It is only those who have neither fired a shot nor heard the shrieks and groans of the wounded who cry aloud for blood, more vengeance, more desolation. War is hell.' - General William Sherman, 1879

    M 1 Reply Last reply
    0
    • M Michael P Butler

      It has been a while since I've done anything like this but, I don't think you need your _kbhit loop. _getch() will wait for a character to be pressed before executing the next statement. This is the MSDN example

      // crt_getch.c
      // compile with: /c
      /* This program reads characters from
      * the keyboard until it receives a 'Y' or 'y'.
      */

      int ch;

      _cputs( "Type 'Y' when finished typing keys: " );
      do
      {
      ch = _getch();
      ch = toupper( ch );
      } while( ch != 'Y' );

      _putch( ch );
      _putch( '\r' ); /* Carriage return */
      _putch( '\n' ); /* Line feed */
      }

      Michael 'War is at best barbarism...Its glory is all moonshine. It is only those who have neither fired a shot nor heard the shrieks and groans of the wounded who cry aloud for blood, more vengeance, more desolation. War is hell.' - General William Sherman, 1879

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

      Thanks... that seems to do the trick :) However I need to do some processing in my _kbhit loop, but maybe I can find a workaround for this.

      N 1 Reply Last reply
      0
      • M MrBean

        I'm developing a Win32 console app in VC++ 6.0 where I have coded a edit box which works fine... however running my console app on Windows 98, my app affects the speed of the computer. I have tracked the problem down to my "main input loop" where my app wait for the user to press a key. The main loop (very simple!) looks something like this :

        BOOL bOK=TRUE;
        int cKey;
        while (bOK)
        {
        // Wait for keystroke...
        while (!_kbhit())
        {
        // Do nothing...
        }

        // Getting the key...
        cKey = \_getch();
        
        // Process the key here...
        // and so on and so on...
        

        }

        So my question is : is there a "proper" way to wait for a input chars in a console app which does not slow down Windows 9x ? Please help, thanks :)

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

        I think _getch() has a built-in wait so you might try removing the call to _kbhit().

        M 1 Reply Last reply
        0
        • M MrBean

          Thanks... that seems to do the trick :) However I need to do some processing in my _kbhit loop, but maybe I can find a workaround for this.

          N Offline
          N Offline
          nfactorial
          wrote on last edited by
          #7

          If you need to do some processing inside the loop, call Sleep( 1 ) at the end of each loop. That should prevent starving windows of cpu time. n!

          M 1 Reply Last reply
          0
          • M MrBean

            I'm developing a Win32 console app in VC++ 6.0 where I have coded a edit box which works fine... however running my console app on Windows 98, my app affects the speed of the computer. I have tracked the problem down to my "main input loop" where my app wait for the user to press a key. The main loop (very simple!) looks something like this :

            BOOL bOK=TRUE;
            int cKey;
            while (bOK)
            {
            // Wait for keystroke...
            while (!_kbhit())
            {
            // Do nothing...
            }

            // Getting the key...
            cKey = \_getch();
            
            // Process the key here...
            // and so on and so on...
            

            }

            So my question is : is there a "proper" way to wait for a input chars in a console app which does not slow down Windows 9x ? Please help, thanks :)

            P Offline
            P Offline
            peterchen
            wrote on last edited by
            #8

            a) you can lower the thread priority while you wait for input b) a Sleep(20) won't hurt c) you can *try* if MsgWaitForMultipleObjects "wakes" your thread when you input a kjey (I doubt that, though)


            "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
            sighist | Agile Programming | doxygen

            M 1 Reply Last reply
            0
            • M MrBean

              As i wrote... It's a Win32 console app :) Not a DOS app but a 32bit console application. Edit : I tried to process the Windopw messages but since my console app does not have a Windows (HWND) handle, I can't do that ;(

              P Offline
              P Offline
              Peter Weyzen
              wrote on last edited by
              #9

              Do the sleep() call. I have console-app based servers -- we do the same thing of a kbhit/getch loop to look for the shutdown keystroke. With a sleep(100) this thread bears no impact on the process.... Thread priority is not a good way to do this, since the crux of the problem is that your thread is always awake and always processing. Threads should not be wasting process time when they don't need to. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer Santa Cruz Networks

              M 1 Reply Last reply
              0
              • N nfactorial

                If you need to do some processing inside the loop, call Sleep( 1 ) at the end of each loop. That should prevent starving windows of cpu time. n!

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

                Thanks, the Sleep worked for me :)

                1 Reply Last reply
                0
                • P peterchen

                  a) you can lower the thread priority while you wait for input b) a Sleep(20) won't hurt c) you can *try* if MsgWaitForMultipleObjects "wakes" your thread when you input a kjey (I doubt that, though)


                  "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                  sighist | Agile Programming | doxygen

                  M Offline
                  M Offline
                  MrBean
                  wrote on last edited by
                  #11

                  Thanks :) I simply added a Sleep which did the trick :)

                  1 Reply Last reply
                  0
                  • D David Crow

                    I think _getch() has a built-in wait so you might try removing the call to _kbhit().

                    M Offline
                    M Offline
                    MrBean
                    wrote on last edited by
                    #12

                    I needed some processing in the _kbhit loop but using Sleep in the loop solved the problem, thanks :)

                    1 Reply Last reply
                    0
                    • P Peter Weyzen

                      Do the sleep() call. I have console-app based servers -- we do the same thing of a kbhit/getch loop to look for the shutdown keystroke. With a sleep(100) this thread bears no impact on the process.... Thread priority is not a good way to do this, since the crux of the problem is that your thread is always awake and always processing. Threads should not be wasting process time when they don't need to. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer Santa Cruz Networks

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

                      I used the Sleep method which worked ! Thanks guys :)

                      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