可以在 ReadFile() 上超时吗? [英] possible to have a Timeout on ReadFile()?

查看:73
本文介绍了可以在 ReadFile() 上超时吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

while(GetExitCodeProcess(processInfo.hProcess, &exitCode)
        && exitCode == STILL_ACTIVE)
{
    ReadFile(defaultSTDIN, chBuf, 1, &dwRead, 0);
    WriteFile(writingEnd, chBuf, 1, &dwWritten, 0);
}

上面代码的问题是,即使通过 processInfo.hProcess 引用的子进程已经退出,我们仍然卡在 while 循环中,因为 ReadFile() 正在等待输入.解决这个问题的最佳方法是什么?

The problem with the code above is that even when the child process referenced through processInfo.hProcess has exited, we are still stuck in the while loop because ReadFile() is waiting for input. Whats the best way to solve this?

推荐答案

您需要的是在打开文件时使用 FILE_FLAG_OVERLAPPED 标志异步读取文件,并指定 OVERLAPPED 结构到 ReadFile 函数.然后你可以等待读取操作和进程终止,并采取适当的行动.

What you need is to read the file asynchronously using the FILE_FLAG_OVERLAPPED flag when opening the file, and specifying an OVERLAPPED structure to the ReadFile function. Then you could wait on both the read operation and the process termination, and act appropriately.

这篇关于可以在 ReadFile() 上超时吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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