1

How can I set timeout for ReadFile and WriteFile operations,

When using interprocess pipes?

Taru
  • 2,562
  • 4
  • 22
  • 30
  • Does this answer your question? [C++ Pipe ReadFile function set timeout in Windows XP](https://stackoverflow.com/questions/7380385/c-pipe-readfile-function-set-timeout-in-windows-xp) – Richard Chambers Oct 31 '21 at 18:31

1 Answers1

5

You have to use the asynchronous version of the function, by specifying FILE_FLAG_OVERLAPPED.

When a timeout is reached you can call CancelIO with the file handle.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • Was afraid of that. Thank you. – Taru Jan 08 '13 at 17:53
  • 3
    Nothing to be afraid of, it's really not hard to use. You don't need another thread or anything: start the unblocking call, `WaitForSingleObject` for 2 seconds, and `CancelIO` if it didn't go through. – Mahmoud Al-Qudsi Jan 08 '13 at 18:02
  • @MahmoudAl-Qudsi there is also `GetOverlappedResultEx` with timeout, seems like it's a bit more convenient and you will also get the number of bytes read. – Alex P. Jul 10 '18 at 09:26