Re: WaitForSingleObject on Console not working???
kimso.zhao@gmail.com wrote:
How can I know if there is input or not from keyboard in console
application? I want to flush buffer to screen every second.
In Unix, select( ) with stdin and timeout can work well( return timeout
every second ), but in Windows, stdin is NOTSOCK, hence select won't
work.
In Windows, WaitForSingleObject( stdinHandle, 1000 ) will return
"WAIT_OBJECT_0" when waiting for input.
How come the following always goes into "WAIT_OBJECT_0"?
Because there is some input on the handle at startup (probably a
FOCUS_EVENT_RECORD), and until you read that, wait calls will return
immediately.
What could I do? Is there any way to use select( ) in Windows with
stdin?
Thanks!
Try playing with this:
#include <windows.h>
#include <stdio.h>
int main()
{
printf("->");
static HANDLE stdinHandle;
// Get the IO handles
// getc(stdin);
stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
while (1)
{
DWORD rc = WaitForSingleObject(stdinHandle, 1000);
if( rc == WAIT_TIMEOUT )
{
printf("Timeout...");
}
else if( rc == WAIT_ABANDONED )
{
printf("WAIT_ABANDONED");
}
else if( rc == WAIT_OBJECT_0 )
{
printf("WAIT_OBJECT_0");
INPUT_RECORD r[512];
DWORD read;
ReadConsoleInput(stdinHandle, r, 512, &read);
printf("Read: %d", read);
}
else if( rc == WAIT_FAILED )
{
printf("Error:%d.", GetLastError());
}
}
return 0;
}
Tom (again!)