命名管道读取超时 [英] Named-pipe reading timeout
问题描述
我正在尝试为我的命名管道的读取操作设置超时.
为了从命名管道中读取内容,我使用了ReadFile
函数.
我读到可以使用SetCommTimeouts
函数为此功能设置超时,但是当我尝试使用它时,出现系统错误1:函数不正确".
这是我的代码(这是客户端):
I'm trying to set a timeout to the reading operation of my named pipe.
In order to read from the named pipe, I'm using the ReadFile
function.
I read that a timeout can be set for this function with the SetCommTimeouts
function but when I try to use it, I get system error 1: "Incorrect function".
Here is my code (this is the client side):
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);
}
我做错了什么吗?或者SetCommTimeouts
方法不应该与管道一起使用?还有其他方法可以使阅读超时吗?
Am I doing something wrong or the SetCommTimeouts
method is not supposed to be used with pipes? Is there any other way to get a reading timeout?
推荐答案
您不能将SetCommTimeouts与命名管道一起使用.如果要超时,则必须使用异步I/O并使用CancelIo
或CancelIoEx
You cannot use SetCommTimeouts with named pipes. If you want timeouts, you will have to use Async I/O and implement the timeout yourself using CancelIo
or CancelIoEx
这篇关于命名管道读取超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!