|
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();
|
|
|
|
|
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...
|
|
|
|
|
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
|
|
|
|
|
I think both use the interrupts.
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
|
|
|
|
|
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.
|
|
|
|
|
int __cdecl _getch (
void
)
{
INPUT_RECORD ConInpRec;
DWORD NumRead;
CharPair *pCP;
int ch = 0;
DWORD oldstate;
if ( chbuf != EOF ) {
ch = (unsigned char)(chbuf & 0xFF);
chbuf = EOF;
return ch;
}
if (_coninpfh == -1)
return EOF;
if ( _coninpfh == -2 )
__initconin();
GetConsoleMode( (HANDLE)_coninpfh, &oldstate );
SetConsoleMode( (HANDLE)_coninpfh, 0L );
for ( ; ; ) {
if ( !ReadConsoleInput( (HANDLE)_coninpfh,
&ConInpRec,
1L,
&NumRead )
|| (NumRead == 0L) )
{
ch = EOF;
break;
}
if ( (ConInpRec.EventType == KEY_EVENT) &&
ConInpRec.Event.KeyEvent.bKeyDown ) {
if ( ch = (unsigned char)ConInpRec.Event.KeyEvent.uChar.AsciiChar )
break;
if ( pCP = _getextendedkeycode( &(ConInpRec.Event.KeyEvent) ) ) {
ch = pCP->LeadChar;
chbuf = pCP->SecondChar;
break;
}
}
}
SetConsoleMode( (HANDLE)_coninpfh, oldstate );
return ch;
}
;)
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
|
|
|
|
|
of course
|
|
|
|
|
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 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
|
|
|
|
|
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...
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
I think no one understood your joke.
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[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.
|
|
|
|
|
It's not your kbhit to consume your processor cycles, but your "while" loop.
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[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.
|
|
|
|
|
hi all,
CMAPIAdviseSink::OnNotify(...) will give the notification of email, Please let me know the steps to be followed to get the Notification of Messages when a Mail is Send to your computer.
Uday kiran
|
|
|
|
|
What do you want?
When the user receives a mail he should be notified or when he is sending a mail and the mail is sent then he should be notified?
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
hi anshuman,
i want to notify when the user receive a mail from enduser.
Uday kiran
|
|
|
|
|
Hello,
I would like to display formatted text from a file (so, with bold, italic, font size, ...) in a control on my application. So far, not a big problem: I can simply use a rich edit control and load a .rtf file and display it in the control.
But, what I need is that this file will be used by the user to specify how he wants the data to be displayed (so he just edit the file to his own taste) and the control need to display information from a class instance (we suppose that the instance is passed to a subclassed rich edit control). So, I was thinking of a kind of tag system: the user specify a tag where he want a class data to be displayed. Something like <GetParam1> . The way it is linked to the member function need still to be defined but it's not the problem here.
So, I will need to parse the string passed to the control, extract the tags an replace them by the 'output' (converted to text) of the member function (or member variable). I was wondering if things like that don't already exist ?
And also, what bothering me is that sometimes the function returns just an integer that need to be displayed in a table. When the user edit the file and has to put a long tag in a table, that could screw the design of the document. Is there a way to put a kind of 'invisible' tags in the rtf document ? They still need to be 'visible' by the user but won't interfere with the layout...
Or maybe somebody has another suggestion ? Any idea is welcome.
Thank you
|
|
|
|
|
It's possible to insert a comment into a RTF file like so:
{\info{\comment ThisIsEmpty;}}
If I remember you can also customize the tags, but it's been a long time since I used RTF.
|
|
|
|
|
Cedric Moonen wrote: Or maybe somebody has another suggestion ? Any idea is welcome.
What about an XML file? Or maybe I do not have a full understanding of what you are after.
"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
|
|
|
|