Console app - how to "keep alive" ?
-
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 :)
-
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 :)
-
I am not sure about what you mean with Win32 console app. Is it a Win32 application, or a console app ? If you have the Win32 code, then just process the message loop and check for keyboard messages. If not, I guess you can use
getchar();
. ~RaGE(); -
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 :)
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
-
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
-
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 :)
I think _getch() has a built-in wait so you might try removing the call to _kbhit().
-
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.
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!
-
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 :)
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 -
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 ;(
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
-
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!
-
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 -
I think _getch() has a built-in wait so you might try removing the call to _kbhit().
-
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