Ido*_*dov 3 c++ windows timeout named-pipes
我正在尝试为我的命名管道的读取操作设置超时.
为了从命名管道读取,我正在使用该ReadFile功能.
我读到可以使用该SetCommTimeouts函数为此函数设置超时但是当我尝试使用它时,我得到系统错误1:"函数不正确".
这是我的代码(这是客户端):
m_pipe = CreateFileA(pipeName, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
if (m_pipe != INVALID_HANDLE_VALUE)
{
DWORD mode = PIPE_READMODE_MESSAGE | PIPE_WAIT;
ok = SetNamedPipeHandleState(m_pipe, &mode, NULL, NULL);
COMMTIMEOUTS cto;
cto.ReadTotalTimeoutConstant = 1000;
BOOL time = SetCommTimeouts(m_pipe, &cto);
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么或者该SetCommTimeouts方法不应该与管道一起使用?有没有其他方法可以获得读取超时?
Arn*_*rno 12
如果超时的目的是不被卡住,forever你可以考虑在定时循环中调用PeekNamedPipe(...).这样您就可以检查是否有任何东西需要不时阅读.或者PeekNamedPipe可以用于确定在执行读取之前管道上的读取是否实际上将获得任何内容.这样可以避免"等待"读取.