I use next code to know when a files is changed in a certain folder:
HANDLE hDir = ::CreateFile(path, FILE_LIST_DIRECTORY, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OVERLAPPED, NULL);
FILE_NOTIFY_INFORMATION returnData[1024];
DWORD returnDataSize = 0;
while(ReadDirectoryChangesW(hDir, returnData, sizeof(returnData), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE, &returnDataSize, NULL, NULL))
{
...
}
ReadDirectoryChangesW blocks the thread until a file changes occurs. Is there any way to stop/force return from this function?
From your description, it sounds like CancelIoEx
should do the trick. Obviously, you need another thread for that, since you're now calling it synchronously. That blocks the calling thread, so you can't do anyting from that thread, not even stop.
I think you need to take a look at this blog post: http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html
It's a lengthy post, but it's very informative and it talks about all the issues associated with this method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With