Article View: borland.public.cppbuilder.nativeapi
Article #2631Re: How to keep a console app busy and responsive?
From: Bob Gonder
Date: Wed, 07 Feb 2007 19:20
Date: Wed, 07 Feb 2007 19:20
68 lines
3230 bytes
3230 bytes
Helmut Giese wrote:
>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.
Have you tried ResetEvent( inHdl ) ?
Maybe FlushConsoleInputBuffer( inHdl ) ?
The docs for ReadConsole don't mention waiting, but the docs for
ReadConsoleInput do. You might try that function.
My own code uses polling with a Sleep( 10 ) because I also check for
UDP messages. Several copies of the program can run with no
notiiceable delays.
I also allow mouse clicks which brings in mouse movements as well
(which I ignore).
GetNumberOfConsoleInputEvents( stdin, &written );
if( 0 == written )
{ ++WaitCounter;
if( WaitCounter > 100 )
{ WaitCounter = 0;
BackgroundProcess();
}else{
Sleep( 10 );
};
}else{
ReadConsoleInput( stdin, &input_record, 1, &written );
// if you want background to only fire after
// 1 second of inactivity
WaitCounter = 0;
};
See Reading Input Buffer Events
http://msdn2.microsoft.com/en-us/library/ms685035.aspx
- - - -
In any case, the usual method these days is to use threads.
Main() could create a CancelEvent, then start the background thread.
When main() wants to exit, it signals CancelEvent to terminate the
background thread.
Main() could also set a global boolean whenever it processes a
keystroke.
The background thread could WaitForSingleObject on the CancelEvent,
and process during WAIT_TIMEOUT, exiting on WAIT_OBJECT_0.
while( 1 )
{ if( WAIT_OBJECT_0 == WaitForSingleObject( CancelEvent, 1000) )
break;
if( GlobalKeyPressed )
{ // key was pressed while we slept
GlobalKeyPressed = false;
}else{
BackgroundProcess();
};
};
This has the advantage of being responsive even when the background
thread is busy.
Whichever thread method you use, you would also want to add
SetConsoleCtrlHandler( &CtrlHandler, TRUE);
to catch the Big Red X and other app closers so you can signal the
thread to exit nicely.
Technical Details
Message-ID:
<is4ks296qj0743bkljpoireniukn37eas7@4ax.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<i9sjs29ki5ej4eijam2hpftu9p7d39fvjp@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,c99d91951e405088 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: Bob Gonder <notbg@notmindspring.invalid> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: How to keep a console app busy and responsive? Date: Wed, 07 Feb 2007 11:20:03 -0800 Organization: MindSpring Enterprises, Inc. Reply-To: notbg@notmindspring.invalid Message-ID: <is4ks296qj0743bkljpoireniukn37eas7@4ax.com> References: <i9sjs29ki5ej4eijam2hpftu9p7d39fvjp@4ax.com> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 69.69.88.225 X-Trace: newsgroups.borland.com 1170875999 69.69.88.225 (7 Feb 2007 11:19:59 -0800) Lines: 71 Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail