我希望ReadConsoleW()
在读取特定字节数后返回。但它没有返回。
如何使ReadConsoleW()
在完成指定字节数的读取后立即返回?
我尝试过的代码如下:
#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)
{};
}
PeekConsoleInput
是非阻塞的,但你必须使用ReadConsoleInput
来消耗输入(或检查输入事件类型以确保ReadConsoleChars
不会阻塞)。 (每当你想要等待控制台事件超时时,只需在控制台句柄上调用WaitForSingleObject
)。 - Anton Kovalenko