I’m trying to write something to replace synchronous WriteFile + CancelSynchronousIo, to add Windows XP support to a program. I chosed asynchronous WriteFile and CancelIo, that is much heavier to satisfy. I wrote small program to test behavior (full code). After some tests I understand that I don’t understand what is going on:
2024-04-24 17:22:26: WriteFile start
2024-04-24 17:22:26: Starting concurrent thread.
2024-04-24 17:22:26: Exiting concurrent thread.
2024-04-24 17:22:26: WriteFile result 1, LastError() = 0
2024-04-24 17:22:26: Wait start
2024-04-24 17:22:26: Second event was signaled.
2024-04-24 17:22:51: Cancel result 1, LastError() = 0
2024-04-24 17:22:51: Wait end
It takes 25 seconds to Write and don’t react on cancel event when I set it. Why? Or sometimes complete event set faster (WaitForMultipleObject return code) and often there is no messages about timeout, but it should appear every 100ms.
My code opens file and start async write operation
const HANDLE hFile = CreateFile(path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
const BOOL result = ::WriteFileEx(m_file.get(), buffer, bytesToWrite, &m_ov, FileIOCompletion);
and after start waiting for complete event
dwEvent = WaitForMultipleObjectsEx(1, waitObjects.data(), FALSE, 1000, FALSE);
I used FileIOCompletion to set complete event from OVERLAPPED (behave like WriteFile)
And sometimes I saw timeout messages, and I think it behaves like expected, but it’s so unpredictible, that I don’t understand is this code correct or not.