WaitForSingleObject on a file handle?

后端 未结 2 406
無奈伤痛
無奈伤痛 2020-12-17 15:01

What happens when you call WaitForSingleObject() on a handle you\'ve created with CreateFile() or _get_osfhandle()?

For reason

2条回答
  •  隐瞒了意图╮
    2020-12-17 15:11

    I found the following links. The concensus seems to me, don't do it.

    • Asynch IO explorer

    Waiting on a file handle

    When an I/O operation is started on an asynchronous handle, the handle goes into a non-signaled state. Therefore, when used in the context of a WaitForSingleObject or WaitForMultipleObjects operation, the file handle will become signaled when the I/O operation completes. However, Microsoft actively discourages this technique; it does not generalize if there exists more than one pending I/O operation; the handle would become signaled if any I/O operation completed. Therefore, although this technique is feasible, it is not considered best practice.

    • Egghead Cafe:

    Use ReadDirectoryChangesW in overlapped mode. WaitForSingleObject can wait on the event in the OVERLAPPED struct.

    You can also use the API WaitForSingleObject() to wait on a file change if you use the following change notification function:
    FindFirstChangeNotification()
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/findfirstchangenotification.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitforsingleobject.asp

    An interesting note on "evilness" of ReadDirectoryChangesW:
    http://blogs.msdn.com/ericgu/archive/2005/10/07/478396.aspx

提交回复
热议问题