【问题标题】:How to read available input without blocking on Windows如何在 Windows 上读取可用输入而不阻塞
【发布时间】:2010-09-04 19:41:26
【问题描述】:

在 Linux 上,我可以在不阻塞进程的情况下读取可用输入:

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");
}

输入源可以是任何东西,例如文件、终端或管道。

如何在 Windows XP 上做到这一点?

谢谢。

【问题讨论】:

    标签: c windows input nonblocking


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可以在 Windows 上通过将FILE_FLAG_OVERLAPPED 传递给CreateFile() 来实现此目的。它看起来与 Linux 不太一样,可能会有一些细微的差别,但它实现了相同的目标。

      查看Synchronous vs. Asynchronous IO 上的 MSDN 页面,它为您提供了有关各种选项的更多详细信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-26
        • 2012-08-16
        • 1970-01-01
        • 1970-01-01
        • 2010-12-08
        • 2019-05-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多