gpt4 book ai didi

c++ - ReadFile 使用 NamedPipe block 虽然使用重叠

转载 作者:行者123 更新时间:2023-11-28 05:02:01 24 4
gpt4 key购买 nike

我正在使用 NamedPipes 在我们的 Windows 应用程序的不同模块之间进行通信。在某一时刻,读取操作可能需要很长时间,所以我们想添加一个超时。我像这样添加了 OVERLAPPED 标志:

pipeHandle = CreateFile(
pipename,
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
0,
nullptr,
OPEN_EXISTING,
0,
nullptr);

那么读取操作是这样的:

    OVERLAPPED overlapped;
ZeroMemory(&overlapped, sizeof(OVERLAPPED));
overlapped.hEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
successful = ReadFile(
pipeHandle,
buffer,
4096 * sizeof(wchar_t),
&numBytesRead,
&overlapped
);
if (successful == FALSE)
{
LOG("Reading was not successful");
if (GetLastError() != ERROR_IO_PENDING)
{
// The function call failed. ToDo: recovery.
LOG_LAST_ERROR("Failed because of Error: ");
return ERROR_READ_FAULT;
}
else
{
// TODO: add a timeout...
successful = GetOverlappedResult(pipeHandle, &overlapped, &numBytesRead, TRUE);
}
}
else
{
LOG("Reading was successful");
// I/O completed:
return ERROR_SUCCESS;
}

现在我希望对 ReadFile 的调用会立即返回,这样我就可以处理可能的超时,但它会阻塞直到调用返回。

我做错了什么?

最佳答案

在您的代码中,FILE_FLAG_OVERLAPPEDdwDesiredAccess 参数中,但它应该在 dwFlagsAndAttributes 参数中指定。

所以,这可能会更好:

pipeHandle = CreateFile(
pipename,
PIPE_ACCESS_DUPLEX,
0,
nullptr,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
nullptr);

关于c++ - ReadFile 使用 NamedPipe block 虽然使用重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45693227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com