如何在Windows上不阻塞的情况下读取可用输入 [英] How to read available input without blocking on Windows

查看:93
本文介绍了如何在Windows上不阻塞的情况下读取可用输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux上,我可以读取可用的输入而不会阻止该过程:

On Linux, I can read available input without blocking the process:

fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK )
char buf[n];
int r = fread(buf, 1, n, stdin);
if (r == 0){
    printf("nothing\n");
}
else {
    printf("read: ");
    fwrite(buf, 1, r, stdout);
    printf("\n");
}

输入原点可以是任何东西,例如文件,终端或管道.

The input origin can be anything, such as a file, a terminal or a pipe.

如何在Windows XP上执行此操作?

How can I do it on Windows XP?

谢谢.

推荐答案

为什么不从第二个线程读取输入?根据您的情况,这可能是一种更简单的方法,而不是使用非阻塞IO.

Why not read the input from a second thread? Depending on your situation, it might be a much easier approach, instead of using non-blocking IO's.

这篇关于如何在Windows上不阻塞的情况下读取可用输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆