suspending program execution. kbhit()
-
I use kbhit for suspending program execution but it consumes processor cycles while(!kbhit()){;} how to do it without consuming cycles
9ine
#include <conio.h>
getch();
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
I use kbhit for suspending program execution but it consumes processor cycles while(!kbhit()){;} how to do it without consuming cycles
9ine
-
One way: while(!kbhit()){Sleep(100);}
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
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! ]
-
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! ]
-
I use kbhit for suspending program execution but it consumes processor cycles while(!kbhit()){;} how to do it without consuming cycles
9ine
It's not your
kbhit
to consume your processor cycles, but your "while" loop.
--[:jig:]-- [My Current Status]
-
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
-
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
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! ]
-
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
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;
}
:);)
-
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;
}
:);)
of course :doh::rolleyes:
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
One way: while(!kbhit()){Sleep(100);}
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
-
It's not your
kbhit
to consume your processor cycles, but your "while" loop.
--[:jig:]-- [My Current Status]
-
so no useful ones for program suspension what about gets() but how to not to print the key you pressed?
9ine
hey, did you read my answer ? getch() works perfectly.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
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! ]
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
-
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
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! ]
-
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! ]
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
-
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! ]
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
-
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
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.