Asynchronous ReadDirectoryChangesW ()?

I want to track changes in a specified directory and receive accurate information about the change. So I decided to use the ReadDirectoryChangesW () function . But I want to use it asynchronously, which means I don't want my worker thread to be blocked if there are no changes in the directory. How can i do this?

Thank!

+1


source to share


2 answers


I'm using ReadDirectoryChangesW () with an I / O completion port, which is good for keeping track of multiple directories because you can have one thread serving the IOCP and handle the results of all your ReadDirectoryChangesW () calls.

First, create the IOCP once by calling CreateIoCompletionPort (). Start a thread that calls GetQueuedCompletionStatus () and touches the changes in your async folder.



Now for each directory you want to monitor. First open your directory descriptor as usual. Then bind it to the IOCP by calling CreateIoCompletionPort () and passing in the IOCP handle (see the docs for details). Finally, call ReadDirectoryChangesW () and pass OVERLAPPED as described in the docs. The call will return and, if successful, the results will be available as the results of GetQueuedCompletionStatus () on the thread you created to serve the IOCP.

+3


source


From MSDN Documentation for ReadDirectoryChnagesW () :



For asynchronous completion, you can receive notification in one of three ways:

  • Using the GetOverlappedResult function. Get notified via GetOverlappedResult, do not specify a completion routine in the lpCompletionRoutine parameter. Be sure to set the hEvent member of the OVERLAPPED structure to a unique event.
  • Using the GetQueuedCompletionStatus function. to receive notification through GetQueuedCompletionStatus, do not specify a completion routine in lpCompletionRoutine. Bind the handle to the hDirectory directory from completion by calling the CreateIoCompletionPort function.
  • Using the completion procedure. Receive notification through completion routines, do not associate directory with completion port. Specify the completion routine in lpCompletionRoutine. This procedure is when the entire operation has been completed or canceled while the thread is in an abnormal waiting state. The hEvent member of the OVERLAPPED structure is not used by the system, so you can use it yourself.
+1


source







All Articles