Non-blocking reading from stdin

I expect to ReadConsoleW()

return after reading a certain number of bytes. But he's not coming back.

How can I make a ReadConsoleW()

return as soon as it finishes reading the number of bytes specified?

The code I tried is here:

#include <stdio.h>
#include <Windows.h>


int main()
{
    //something is being written to stdin.
    Sleep(2000);
    int b;
    int r;
    //read 3 wide character
    ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), &b, 3*sizeof(TCHAR), (LPDWORD)&r, NULL);
    //problem: no returns until enter pressed
    putc(b,stdout);
    while(1)
    {};
}

      

+3


source to share


2 answers


Use SetConsoleMode

to disable the flag ENABLE_LINE_INPUT

. No line editing, but it won't wait for Enter to be pressed.



Note that you cannot read three WCHAR

in int

.

+2


source


Let's also look at asynchronous I / O on Windows using ReadFile / WriteFile. See MSDN on Asynchronous I / O



It's a little more complicated, but you have what you want.

0


source







All Articles