🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2626

How to keep a console app busy and responsive?

#2626
From: Helmut Giese
Date: Wed, 07 Feb 2007 15:55
55 lines
2647 bytes
Hello out there,
a console mode app of mine needs to periodically perform some action
while still being able to react to input from the user.
I thought that the combination of getting the handle to the input
console and then doing a WaitForSingleObject with a suitable timeout
would accomplish what I need - but it doesn't work.

The program below (when started from a console)
1) repeatedly prints out the message "No key ...." UNTIL any key is
pressed, at which point
2) it ALWAYS goes into the 'ReadConsole' after the WaitForSingleObject
call, whether a key is pressed or not. Of course it blocks there if no
key is pressed - the message "No key ..." is never again seen.

When started via Start | Run, step (1) from above is skipped.

Any help to get me on the right track will be greatly appreciated,
Best regards
Helmut Giese
		-----
#include <windows.h>
#include <stdio.h>

#pragma argsused
int main(int argc, char* argv[])
{
   HANDLE inHdl, outHdl;
   char inBuff[80];
   int done = 0;
   DWORD cnt, dwRes;

   // get console handle
   inHdl = GetStdHandle(STD_INPUT_HANDLE);
   SetConsoleMode(inHdl, 0);
//   SetConsoleMode(inHdl, ENABLE_PROCESSED_INPUT);

   printf("Trying to be busy & responsive in console mode\n");
   while ( !done ) {
      dwRes = WaitForSingleObject(inHdl, 1000);
      if ( dwRes == WAIT_OBJECT_0 ) {
         // input available
         ReadConsole(inHdl, inBuff, 1, &cnt, 0);
         switch ( inBuff[0] ) {
           case '1': printf("Received key '1'\n"); break;
           case '2': printf("Received key '2'\n"); break;
           case 'q': done = 1; break;
         }
      } else {
         printf("No key - going to do other work\n");
      }
   }

   return 0;
}
		-----
Technical Details
Message-ID: <i9sjs29ki5ej4eijam2hpftu9p7d39fvjp@4ax.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,c99d91951e405088,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Helmut Giese <hgiese@ratiosoft.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: How to keep a console app busy and responsive?
Date: Wed, 07 Feb 2007 16:55:54 +0100
Message-ID: <i9sjs29ki5ej4eijam2hpftu9p7d39fvjp@4ax.com>
X-Newsreader: Forte Free Agent 1.93/32.576 Deutsch (Standard)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 217.91.125.68
X-Trace: newsgroups.borland.com 1170863751 217.91.125.68 (7 Feb 2007 07:55:51 -0800)
Lines: 56
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail